Skip to content

Account for AgentOperator spend on failed runs and across retries - #73706

Merged
Lee-W merged 3 commits into
apache:mainfrom
astronomer:common-ai-agent-usage-on-failure
Sep 30, 2026
Merged

Lee-W merged 3 commits into
apache:mainfrom
astronomer:common-ai-agent-usage-on-failure

Conversation

@Lee-W

@Lee-W Lee-W commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Why

When an agent run raised (a usage limit, a tool crash, a timeout), the usage it had already incurred was never logged or pushed to XCom, so the spend of failed attempts was invisible. usage_limits was also enforced per attempt, so a task with retries could spend (retries + 1) times the configured cap, which defeats a cost limit meant to bound spend.

What

The task state store (Airflow 3.3+) survives retries, which makes it possible to carry spend across attempts. Durable replay used to recount the usage of cached steps; against a cumulative budget that would double-count already-paid work and block retries that make no new calls.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: [Claude] following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@Lee-W
Lee-W force-pushed the common-ai-agent-usage-on-failure branch 2 times, most recently from a81bf05 to c9b5330 Compare September 29, 2026 09:21
@Lee-W
Lee-W marked this pull request as ready for review September 29, 2026 09:22
@Lee-W
Lee-W requested a review from amoghrajesh September 29, 2026 09:23
@Lee-W
Lee-W force-pushed the common-ai-agent-usage-on-failure branch 2 times, most recently from edfdf92 to 9261e9c Compare September 29, 2026 09:24

@amoghrajesh amoghrajesh 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.

Nice fix, thanks @Lee-W.

Compat looks solid too.

  1. Gated correctly - anything before 3.3, or usage_limits=None, behaves exactly as before.
  2. The usage xcom keeps its old shape and its old meaning (this attempt's own spend, via the attempt_usage delta), even when the cumulative budget is active underneath.
  3. A task already paused for tool approval before this lands still resumes fine - attempt_usage defaults to None for a payload saved by the old code.

Looks good with two nits.

When an agent run raised (a usage limit, a tool crash, a timeout), the
usage it had already incurred was never logged or pushed to XCom, so the
spend of failed attempts was invisible. usage_limits was also enforced per
attempt, so a task with retries could spend (retries + 1) times the
configured cap, which defeats a cost limit meant to bound spend.

The task state store (Airflow 3.3+) survives retries, which makes it
possible to carry spend across attempts. Durable replay used to recount
the usage of cached steps; against a cumulative budget that would
double-count already-paid work and block retries that make no new calls.
@Lee-W
Lee-W force-pushed the common-ai-agent-usage-on-failure branch from 9261e9c to e10b150 Compare September 30, 2026 02:55
@Lee-W
Lee-W merged commit bfa2fe0 into apache:main Sep 30, 2026
83 checks passed
@Lee-W
Lee-W deleted the common-ai-agent-usage-on-failure branch September 30, 2026 05:11
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.

2 participants