Skip to content

Push LLMOperator's token usage and cost to XCom - #74355

Merged
pierrejeambrun merged 4 commits into
apache:mainfrom
astronomer:common-ai-llm-usage-xcom
Oct 19, 2026
Merged

pierrejeambrun merged 4 commits into
apache:mainfrom
astronomer:common-ai-llm-usage-xcom

Conversation

@pierrejeambrun

@pierrejeambrun pierrejeambrun commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

AgentOperator already exposes its request/token/cost counts and a run_id on XCom, and stamps the task-instance identity on its GenAI spans. LLMOperator computed the same usage for its own log summary but never published it, so there was no way to see what an LLMOperator run cost, or to join it to its trace, without reading task logs. The AI Model tab reads the usage XCom, so for @task.llm tasks it could only show a bare model name.

This brings LLMOperator (and @task.llm) in line with AgentOperator, the same keys in the same shape, so tooling built on them works for both operators.

A run stopped by a cost_limit is the one where its spend matters most, so the partial usage is logged and pushed on a failed run too. Under the cross-attempt usage budget (Airflow >= 3.3 with usage_limits set), the usage XCom is this attempt's own usage, not the running total, matching AgentOperator.

The other LLM* operators (LLMBranchOperator, LLMSQLQueryOperator, LLMSchemaCompareOperator, LLMFileAnalysisOperator) override execute() instead of calling LLMOperator's, so they are unchanged here. That is follow-up work.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.7, Opus 5.5)

Generated-by: Claude Code (Opus 4.7, Opus 5.5) following the guidelines

@pierrejeambrun

pierrejeambrun commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Could do the same for LLMBranchOperator since we now push the model name for this operator. (probably a follow up)

@pierrejeambrun
pierrejeambrun marked this pull request as draft October 8, 2026 12:34
@pierrejeambrun
pierrejeambrun force-pushed the common-ai-llm-usage-xcom branch from b001a6b to 55fe90e Compare October 14, 2026 14:47
@pierrejeambrun
pierrejeambrun marked this pull request as ready for review October 14, 2026 15:26

@kaxil kaxil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, LGTM

@kaxil

kaxil commented Oct 16, 2026

Copy link
Copy Markdown
Member

@pierrejeambrun Conflicts :(

AgentOperator already exposes request/token/cost counts and a run id on
XCom and stamps the task-instance identity on its GenAI spans; LLMOperator
computed the same usage for its log summary but never published it, so
there was no way to see what an LLMOperator run cost, or join it to its
trace, without reading task logs. A run stopped by a cost limit is the one
where this matters most, so the usage is pushed on a failed run too.
With do_xcom_push=False, the usage XCom was the only record of what a
failed run spent, so a run stopped by a cost limit left no trace of its
spend anywhere. Logging it matches what AgentOperator already does.
…b docs

The feature map still said only @task.agent tasks show token counts and
cost in the AI Model tab, which no longer holds now that LLMOperator
pushes the same usage XCom.
Everything that runs after a run raises is reporting only and must never
replace the run's own exception, the same rule AgentOperator follows for
its failed-run report.
@pierrejeambrun
pierrejeambrun force-pushed the common-ai-llm-usage-xcom branch from bfeb976 to fe77aad Compare October 19, 2026 08:49
@pierrejeambrun
pierrejeambrun merged commit 48a0f8e into apache:main Oct 19, 2026
73 checks passed
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