Repository navigation
Add a budget argument to AnthropicAgentSessionOperator - #71462
Merged
Merged
Conversation
kaxil
force-pushed
the
anthropic-session-budget-param
branch
from
August 11, 2026 19:58
07978a7 to
810dd51
Compare
kaxil
marked this pull request as ready for review
August 11, 2026 19:58
Contributor
There was a problem hiding this comment.
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
budgetparameter toAnthropicAgentSessionOperator, include it intemplate_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, plusAnthropicHook.update_session()for partial updates (omitted vsNonesemantics). - 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
requested review from
amoghrajesh,
ashb,
bbovenzi,
bugraoz93,
dheerajturaga,
eladkal,
ephraimbuddy,
jason810496,
jedcunningham,
jscheffl,
mobuchowski,
pierrejeambrun,
potiuk,
shahar1,
shubhamraj-git,
uranusjr and
vincbeck
as code owners
August 11, 2026 21:02
kaxil
removed request for
XD-DENG,
amoghrajesh,
bbovenzi,
bugraoz93,
choo121600,
dheerajturaga,
eladkal,
ephraimbuddy,
guan404ming,
henry3260,
jason810496,
jedcunningham,
jscheffl,
mobuchowski,
pierrejeambrun,
rawwar,
shahar1,
shubhamraj-git,
uranusjr,
vatsrahul1001 and
vincbeck
August 11, 2026 21:46
kaxil
force-pushed
the
anthropic-session-budget-param
branch
from
August 12, 2026 06:38
487766b to
19f4906
Compare
Lee-W
reviewed
Aug 12, 2026
kaxil
force-pushed
the
anthropic-session-budget-param
branch
from
August 12, 2026 11:07
19f4906 to
2b5601c
Compare
Contributor
|
@kaxil I wanna take a look at this one too, if you could hold it till tomorrow, that'd be great. |
Lee-W
approved these changes
Aug 13, 2026
…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.
18 of 91 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Session budgets were reachable only as a raw payload through
session_kwargs: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
budgetis now a first-class templated argument. A number or numeric string is read asUS dollars, so the ceiling can come from a Variable and change without editing the Dag:
A mapping is still accepted verbatim for a payload shape the provider has not caught up
with.
Conversion runs through
Decimalbuilt fromstr(), never binary float, so0.07yields"7"rather than a rounding artifact. An amount finer than a cent is rejected instead ofsilently 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_sessionfills a gap that predates budgets -- the hook wrappedcreate, retrieve and archive but never update. It forwards only the keywords the caller
passes, because the API distinguishes omitted (preserve) from
None(clear):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
amountis minorunits, so
budget=25and{"amount": "25"}are 100x apart. Both are documented at thepoint of use.
AnthropicSessionBudgetExceededfires onmessageruns only. Anoutcomerun isjudged from
outcome_evaluationsbefore the idle event is consulted, so a budget stop theresurfaces as the outcome verdict and the generic
AnthropicAgentSessionError.Retries multiply spend. Each Airflow retry starts a new session with a fresh budget, so
retries=2with a $25 ceiling can spend $75. Preferretries=0, or raise the budget on theexisting session rather than retrying.