Why Common AI¶
You can call a model from Airflow without this provider: put an agent framework or a
vendor SDK in a @task and it runs. The trouble starts when the agent does real work.
On retry, every model and tool call runs again and is billed again. A spend limit resets
with each attempt. Pausing for a person means wiring up a separate approval task, and
giving the agent tools means writing them yourself. This provider handles those cases
for agents, and its retry policies bring a model’s judgment to any task that fails.
For a single model call with no tools, where a retry costs little, a plain @task is
enough; see When a plain task is enough.
The same job, two ways¶
An agent reads a reports bucket, queries the orders table and says whether the numbers
agree. Without this provider, a Pydantic AI agent in a @task looks like this, with
the model key in the worker’s environment and the tools written by hand:
from pydantic_ai import Agent
from airflow.sdk import BaseHook, ObjectStoragePath, dag, task
@dag
def check_report_plain():
@task(retries=2)
def check_report() -> str:
# The key comes from OPENAI_API_KEY in the worker's environment.
agent = Agent(
"openai:gpt-5",
instructions=(
"You check reports against the warehouse. Read the report files, query "
"the orders table, and say whether the numbers agree."
),
)
@agent.tool_plain
def query(sql: str) -> list:
# Runs whatever SQL the model writes, on any table the connection can read.
return BaseHook.get_connection("warehouse").get_hook().get_records(sql)
@agent.tool_plain
def read_report(name: str) -> str:
reports = ObjectStoragePath("s3://acme-reports/finance/", conn_id="aws_default")
return (reports / name).read_text()
return agent.run_sync("Does the September report's revenue match the orders table?").output
check_report()
This is the same job with AgentOperator. Its system_prompt is passed to the agent
as Pydantic AI instructions:
from airflow.providers.common.ai.operators.agent import AgentOperator
from airflow.providers.common.ai.toolsets import ObjectStorageToolset
from airflow.providers.common.ai.toolsets.sql import SQLToolset
from airflow.providers.common.compat.sdk import dag
@dag(tags=["example"])
def example_why_common_ai():
AgentOperator(
task_id="check_report",
prompt="Does the September report's revenue match the orders table?",
llm_conn_id="pydanticai_default",
system_prompt=(
"You check reports against the warehouse. Read the report files, query the "
"orders table, and say whether the numbers agree."
),
toolsets=[
SQLToolset(db_conn_id="warehouse", allowed_tables=["orders"]),
ObjectStorageToolset("s3://acme-reports/finance/", conn_id="aws_default"),
],
retries=2,
# A retry replays the model and tool calls that already finished.
durable=True,
# On Airflow 3.3+ one total covers every attempt of the task instance.
usage_limits={"request_limit": 30, "total_tokens_limit": 200_000},
)
Both need an LLM connection or key, a warehouse connection with an orders table,
and a bucket of report files. What differs:
The tools. The hand-written
queryruns any SQL the model writes, includingDELETE, on any table the connection can reach, and returns every row it gets.SQLToolsetallows only theorderstable, rejects statements that write, and caps the rows and bytes it returns.ObjectStorageToolsetkeeps reads under the prefix you gave it. Both run Airflow’s secrets masker over results, which hides values Airflow knows are secret, such as connection passwords, not personal data in your tables.The model. The plain task names a vendor in code and reads its key from the environment.
AgentOperatortakes both from a connection, so moving to another vendor or adding a fallback is a connection change. See Supported model providers.A retry. The plain task starts again from the beginning and pays for every call again.
durable=Truereplays the calls that finished and continues from there. Before Airflow 3.3 it needs[common.ai] durable_cache_pathset; see Durable execution.Spend. In the plain task, Pydantic AI’s default limit of 50 model requests applies to each attempt on its own, so three attempts can make up to 150. With
usage_limitson Airflow 3.3 and later, one total covers all three attempts, and the task fails once it passes 30 requests or 200,000 tokens. Calls replayed from the durable cache do not count.The task log.
AgentOperatorlogs each tool call it makes.
Pydantic AI lists what AgentOperator adds on top of an agent you
run yourself, including when you keep your own agent and use only this provider’s
toolsets.
What a plain task leaves to you¶
When this happens |
What this provider does |
Where it is documented |
|---|---|---|
The task retries after the agent has already made model and tool calls. |
|
|
A run must not spend past a limit, retries included. |
|
|
A person has to approve, edit or reject a model’s output before the Dag goes on. |
|
|
A person has to approve a tool call before it runs, such as a refund or a write to a production table. |
A tool marked with Pydantic AI’s approval API pauses |
|
A reviewer wants to send an agent’s answer back with feedback, several times. |
|
|
The agent needs your databases, object storage or an API you already have a hook for. |
|
|
A model vendor is down. |
|
|
Thousands of prompts need an answer, and not within seconds. |
The batch operator sends them through the OpenAI or Anthropic batch API at
roughly half the per-token cost. A retry re-attaches to the batch already
submitted if the prompts, model, output schema and connection are unchanged.
With |
The provider runs on Airflow 2.11 and later; a row that needs a newer Airflow says so.
Decide whether a failed task retries¶
Retry policies work on any task, including one that never calls a model. A retry policy decides whether a failed task should retry, and the two in this provider ask a model:
from airflow.providers.common.ai.policies.retry import LLMRetryPolicy
from airflow.sdk import task
@task(retries=5, retry_policy=LLMRetryPolicy(llm_conn_id="pydanticai_default"))
def call_partner_api(): ...
When the task fails, the model reads the exception and decides whether to retry and how
long to wait, so it can decide to wait out a rate limit and to fail a revoked credential
at once. The exception text goes through the secrets masker before the model sees it,
and fallback_rules decide when the model cannot be reached. ClassifierRetryPolicy
has a cheaper model pick from categories you define, and can hand the cases it is unsure
of to an LLMRetryPolicy. Both need Airflow 3.3 or later. See Retry policies.
When a plain task is enough¶
One model call with no tools, where running it again on retry costs little.
An agent you already run in another framework, where you need none of the above. You can still give it this provider’s toolsets; see Agent frameworks.
A vendor’s native API this provider does not wrap, such as the OpenAI Responses API or a vendor’s own embeddings endpoint. Use the vendor’s provider; the table on the landing page lists them.