You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jobs@airflow.apache.org by GitBox <gi...@apache.org> on 2026/08/11 20:39:10 UTC
[GH] (airflow/anthropic-session-usage-xcom): Workflow run "Tests (AMD)" failed!
The GitHub Actions job "Tests (AMD)" on airflow.git/anthropic-session-usage-xcom has failed.
Run started by GitHub user kaxil (triggered by kaxil).
Head commit for run:
3e44b9ebe2eee13423f3cada9a06406e3db33a79 / Kaxil Naik <ka...@gmail.com>
Record Anthropic agent session token usage and cost in XCom
A session budget is a stop trigger, not a spend cap: the ceiling is
checked between model requests, so a request already in flight runs to
completion. Measured against the live API, a $0.01 ceiling admitted
between $0.32 and $0.61 of usage across four runs. Setting a budget
therefore tells you nothing about what a run actually cost, and the
operator returned only a session ID.
``AnthropicAgentSessionOperator`` now pushes the session's usage to XCom
under ``usage``, making cost per Dag run queryable:
{"input_tokens": 827, "output_tokens": 14002,
"cache_read_input_tokens": 0,
"cache_creation": {"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 0},
"server_tool_use": {"web_search_requests": 0,
"web_fetch_requests": 0},
"active_seconds": 213.7,
"list_cost": {"amount": "36", "currency": "USD"},
"try_number": 1}
``AnthropicHook.get_session_usage`` flattens the SDK models to plain
scalars so the value survives XCom serialization. ``amount`` stays the
API's minor-unit string rather than becoming a float, because a cost
figure must not pick up binary rounding. ``list_cost`` is ``None`` when
usage includes a model with no list price -- and that is exactly when a
caller has to price the run from usage instead, so every billable
dimension is reported: cache writes are billed above base input and
server tool calls are billed per request.
Usage is recorded on failure as well as success, since a budget-stopped
session is precisely the one whose spend needs recording. Teardown runs
first on the failure paths: it is the time-critical call, and
``sessions.archive`` returns the session carrying its final usage, so
``summarize_usage`` reports spend without a second request against an API
that may be why the task is failing. If archiving fails, usage falls back
to a fetch. The whole push is best effort -- it runs immediately before
re-raising, so anything that throws there would otherwise replace the
exception the task should fail with.
Airflow clears XCom at the start of each attempt, so ``usage`` holds the
final attempt only; ``try_number`` records which, and the docs say total
spend across retries must be summed from the session records.
Verified end to end against the live API: a real deferred run resumed by
a real triggerer failed with AnthropicSessionBudgetExceeded and left the
usage above in XCom, read back over the REST API.
Report URL: https://github.com/apache/airflow/actions/runs/31530602457
With regards,
GitHub Actions via GitBox
---------------------------------------------------------------------
To unsubscribe, e-mail: jobs-unsubscribe@airflow.apache.org
For additional commands, e-mail: jobs-help@airflow.apache.org