Skip to content

Add a budget argument to AnthropicAgentSessionOperator - #71462

Merged
kaxil merged 1 commit into
mainfrom
anthropic-session-budget-param
Aug 13, 2026
Merged

kaxil merged 1 commit into
mainfrom
anthropic-session-budget-param

Conversation

@kaxil

@kaxil kaxil commented Aug 11, 2026 •

Copy link
Copy Markdown
Member

Problem

Session budgets were reachable only as a raw payload through session_kwargs:

session_kwargs={
    "budget": {"type": "limit", "max_list_cost": {"amount": "2500", "currency": "USD"}}
}

Minor units as an integer string is the right wire format but a poor authoring surface, and
being buried in a passthrough dict meant the ceiling could not be templated.

Solution

budget is now a first-class templated argument. A number or numeric string is read as
US dollars, so the ceiling can come from a Variable and change without editing the Dag:

AnthropicAgentSessionOperator(
    task_id="research",
    agent_id="agt_...",
    environment_id="env_...",
    message="Summarise yesterday's incidents.",
    budget=25.00,
    retries=0,
)

A mapping is still accepted verbatim for a payload shape the provider has not caught up
with.

Conversion runs through Decimal built from str(), never binary float, so 0.07 yields
"7" rather than a rounding artifact. An amount finer than a cent is rejected instead of
silently rounded: quietly changing an amount of money is worse than refusing it. The
operator normalizes before creating the session, so a bad amount fails without leaving a
server-side container allocated. Setting the budget both ways at once is rejected rather
than one silently winning.

AnthropicHook.update_session fills a gap that predates budgets -- the hook wrapped
create, retrieve and archive but never update. It forwards only the keywords the caller
passes, because the API distinguishes omitted (preserve) from None (clear):

hook.update_session(session_id, budget=50.00)  # raise the ceiling
hook.update_session(session_id, budget=None)  # remove it entirely

Removing it is the only escape for a session stopped by a model with no list price, which
raising the ceiling cannot unblock. The same call carries the agent={"tools": [...]}
replacement behind the mid-conversation-tool-changes beta.

Gotchas

Units differ between the two forms. A scalar is dollars; a mapping's amount is minor
units, so budget=25 and {"amount": "25"} are 100x apart. Both are documented at the
point of use.

AnthropicSessionBudgetExceeded fires on message runs only. An outcome run is
judged from outcome_evaluations before the idle event is consulted, so a budget stop there
surfaces as the outcome verdict and the generic AnthropicAgentSessionError.

Retries multiply spend. Each Airflow retry starts a new session with a fresh budget, so
retries=2 with a $25 ceiling can spend $75. Prefer retries=0, or raise the budget on the
existing session rather than retrying.

@kaxil kaxil changed the title Add a budget argument to AnthropicAgentSessionOperator and update_session to the hook Add a budget argument to AnthropicAgentSessionOperator Aug 11, 2026
@kaxil
kaxil force-pushed the anthropic-session-budget-param branch from 07978a7 to 810dd51 Compare August 11, 2026 19:58
@kaxil
kaxil marked this pull request as ready for review August 11, 2026 19:58
@kaxil
kaxil requested a review from gopidesupavan as a code owner August 11, 2026 19:58
@kaxil
kaxil requested a lite review from Copilot August 11, 2026 19:58
@kaxil
kaxil requested a review from Lee-W August 11, 2026 20:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the authoring surface for Anthropic Managed Agents session spend ceilings by promoting budget to a first-class, templated operator argument (USD scalar or raw payload mapping), and by adding hook support for updating an existing session (including raising/clearing budgets).

Changes:

  • Add a budget parameter to AnthropicAgentSessionOperator, include it in template_fields, and eagerly normalize/reject invalid inputs before allocating a session.
  • Introduce build_budget() to normalize USD amounts into the API’s minor-unit payload, plus AnthropicHook.update_session() for partial updates (omitted vs None semantics).
  • Add unit tests for budget normalization, operator behavior, and hook update/create budget handling; update provider docs accordingly.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
providers/anthropic/src/airflow/providers/anthropic/operators/agent.py Adds budget as a templated operator arg with conflict detection vs session_kwargs and eager normalization.
providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py Adds BudgetSpec, build_budget() normalization, create-session budget handling, and new update_session() helper.
providers/anthropic/tests/unit/anthropic/operators/test_agent.py Adds operator unit tests covering scalar/mapping budgets, conflict rejection, and pre-allocation validation.
providers/anthropic/tests/unit/anthropic/hooks/test_anthropic.py Adds tests for build_budget(), update_session() key-passthrough semantics, and create-session budget normalization/drop-None.
providers/anthropic/docs/operators/anthropic.rst Documents the new budget parameter, templating, raw-payload escape hatch, and update/clear workflow.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@kaxil
kaxil force-pushed the anthropic-session-budget-param branch from 19f4906 to 2b5601c Compare August 12, 2026 11:07
@amoghrajesh

Copy link
Copy Markdown
Contributor

@kaxil I wanna take a look at this one too, if you could hold it till tomorrow, that'd be great.

…sion to the hook

Session budgets were reachable only as a raw payload through
``session_kwargs``:

    session_kwargs={"budget": {"type": "limit",
                    "max_list_cost": {"amount": "2500", "currency": "USD"}}}

Minor units as an integer string is the right wire format but a poor
authoring surface, and being buried in a passthrough dict meant it could
not be templated.

``budget`` is now a first-class templated argument. A number or numeric
string is read as US dollars (``budget=25.00``), so the ceiling can come
from a Variable and change without editing the Dag. A mapping is still
accepted verbatim, which keeps currencies and budget types the provider
does not model usable without a provider release.

Conversion runs through ``Decimal`` built from ``str()``, never binary
float, so ``0.07`` yields ``"7"`` rather than a rounding artifact. An
amount finer than a cent is rejected instead of silently rounded --
quietly changing an amount of money is worse than refusing it. The
operator normalizes before creating the session, so a bad amount fails
without leaving a server-side container allocated. Setting the budget
both ways at once is rejected rather than one silently winning.

``AnthropicHook.update_session`` fills a gap that predates budgets: the
hook wrapped create/retrieve/archive but never update. It forwards only
the keywords the caller passes, because the API distinguishes *omitted*
(preserve) from ``None`` (clear) -- so ``budget=None`` removes a ceiling,
which is the only escape for a session stopped by a model with no list
price. It also carries the ``agent={"tools": [...]}`` replacement behind
the mid-conversation-tool-changes beta.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants