Track cost and usage
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -65,7 +65,7 @@
`total_cost_usd`| Included. Counts subagent requests alongside the top-level loop
`modelUsage` / `model_usage`| Included. Counts subagent requests alongside the top-level loop, broken down by model
-In [single message input mode](</docs/en/agent-sdk/streaming-vs-single-mode#single-message-input>), when background subagents are still running at the end of the final turn, Claude Code waits for them, up to the cap described in [background tasks at exit](</docs/en/headless#background-tasks-at-exit>), before emitting the result. The result’s `total_cost_usd`, `duration_api_ms`, and `modelUsage`, or `model_usage` in Python, include the work done during that wait. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
+In [single message input mode](</docs/en/agent-sdk/streaming-vs-single-mode#choose-single-shot-or-streaming-input>), Claude Code waits for background subagents still running at the end of the final turn before emitting the result. The wait lasts up to the cap described in [background tasks at exit](</docs/en/headless#background-tasks-at-exit>). The result’s `total_cost_usd`, `duration_api_ms`, and `modelUsage`, or `model_usage` in Python, include the work done during that wait. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
TypeScript
Show diff (172 lines)
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -8,11 +8,7 @@
One billing rule the SDK does model is [data residency pricing](<https://platform.claude.com/docs/en/about-claude/pricing#data-residency-pricing>). When a response’s `usage` reports `inference_geo: "us"`, the SDK multiplies the list price of that response’s tokens by 1.1. Per-request fees such as web search aren’t multiplied. Requires TypeScript Agent SDK v0.3.239 or later, or Python Agent SDK v0.2.144 or later.Use these fields for development insight and approximate budgeting. For authoritative billing, see [Manage costs for your organization](</docs/en/costs#manage-costs-for-your-organization>). Don’t bill end users or trigger financial decisions from these fields.
-##
-
-
-
-Understand token usage
+## Understand token usage
The TypeScript and Python SDKs expose the same usage data with different field names:
@@ -39,11 +35,7 @@
When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python. If you only need the estimated total, you can ignore the per-step usage and read this single value.If you make multiple independent `query()` calls, each result reflects only the cost of that individual call. A call that resumes a session also counts the session’s earlier spend.In streaming input mode, each turn emits its own result message. See Track costs in streaming input mode for how to read call totals in that mode.
-##
-
-
-
-Track costs in streaming input mode
+## Track costs in streaming input mode
In [streaming input mode](</docs/en/agent-sdk/streaming-vs-single-mode>), one `query()` call carries multiple user turns and each turn emits its own result message. The result fields differ in scope:
@@ -58,11 +50,7 @@
To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. In Python, the SDK likewise emits a `ConversationResetMessage`. Before Python SDK v0.2.137, the Python iterator dropped that message, so on those versions count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd` (TypeScript) or `max_budget_usd` (Python) counts only the call’s own spend: totals restored from a resumed session don’t count against it, and a `/clear` starts the budget over.
-##
-
-
-
-Get the total cost of a query
+## Get the total cost of a query
The result message, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python, marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. A call that resumes a session also counts the session’s earlier spend. Two caveats apply when you read the value:
@@ -117,19 +105,11 @@
To bound how much subagents can add to `total_cost_usd`, set the [depth, concurrency, and spend limits](</docs/en/agent-sdk/subagents#cap-subagent-depth-concurrency-and-spend>) on the query.
-##
-
-
-
-Track per-step and per-model usage
+## Track per-step and per-model usage
The examples in this section use TypeScript field names. In Python, the equivalent fields are [`AssistantMessage.usage`](</docs/en/agent-sdk/python#assistantmessage>) and `AssistantMessage.message_id` for per-step usage, and [`ResultMessage.model_usage`](</docs/en/agent-sdk/python#resultmessage>) for per-model breakdowns.
-###
-
-
-
-Track per-step usage
+### Track per-step usage
Each assistant message contains a nested `BetaMessage` (accessed via `message.message`) with an `id` and `usage` object with token counts. When Claude uses tools in parallel, multiple messages share the same `id` with identical usage data. Track which IDs you’ve already counted and skip duplicates to avoid inflated totals.
@@ -170,11 +150,7 @@
console.log(`Input tokens: ${totalInputTokens}`);
console.log(`Output tokens: ${resultOutputTokens}`);
-###
-
-
-
-Break down usage per model
+### Break down usage per model
The result message includes [`modelUsage`](</docs/en/agent-sdk/typescript#modelusage>), a map of model name to per-model token counts and cost. This is useful when you run multiple models (for example, Haiku for subagents and Opus for the main agent) and want to see where tokens are going. Each entry’s `costBasis` says which price table priced that model’s latest request: `list` for list price, `managed` for a [`modelPricing`](</docs/en/settings-reference#modelpricing>) table, or `unknown` when neither matched the model ID. The field requires Claude Code v2.1.246 or later. The following example runs a query and prints the cost and token breakdown for each model used:
@@ -199,11 +175,7 @@
console.error(`Session ended with an error: ${error}`);
}
-##
-
-
-
-Accumulate costs across multiple calls
+## Accumulate costs across multiple calls
Each `query()` call returns `total_cost_usd` on its results. How you combine the values depends on whether the calls share a session:
@@ -275,27 +247,15 @@
asyncio.run(main())
-##
-
-
-
-Handle errors, caching, and output token counts
+## Handle errors, caching, and output token counts
For accurate cost tracking, account for the placeholder output count on assistant messages, the tokens a failed conversation consumed, and cache token pricing.
-###
-
-
-
-Read output tokens from the result message
+### Read output tokens from the result message
Claude Code builds each assistant message from the usage the API reported when the response began, so the message’s `output_tokens` is only the count the API had reported at `message_start`, before the response was generated. One API response can produce several assistant messages, and every one of them carries that same placeholder. The API reports the real output count at the end of the response, and Claude Code adds it to the result message. Read output tokens from the result’s `usage`, or from `modelUsage` for a per-model breakdown.
-###
-
-
-
-Track costs on failed conversations
+### Track costs on failed conversations
Both success and error result messages include `usage` and `total_cost_usd`; in Python both fields are typed as optional, so check that they aren’t `None` before you read them. If a conversation fails midway, you still consumed tokens up to the point of failure. Read cost data from every result message, whether its `subtype` is `success` or one of the error subtypes. On some error results, `usage` reports less than the call spent:
@@ -304,11 +264,7 @@
Where you have the choice, account from `total_cost_usd` or `modelUsage` rather than `usage`.
-###
-
-
-
-Recover totals after a session crash
+### Recover totals after a session crash
When the Claude Code process crashes, it emits a final `error_during_execution` result and exits, in single-shot and streaming input mode alike. That result may carry zeroed `usage`, `total_cost_usd`, and `modelUsage`, so recover the call’s totals from what arrived before it. Step 1 recovers the full totals whenever an earlier result exists; the fallback in step 2 recovers only the main loop’s input and cache tokens.
@@ -318,11 +274,7 @@
* The turn before the crash was the `/clear` itself, so its result covers only the reset.
2. Sum the `usage` on the assistant messages instead, counting each API response once, as the Track per-step usage example does. In single-shot mode, sum all of them; in streaming input mode, sum the ones that arrived after the last result. This gives you the main loop’s input and cache tokens. Subagent usage isn’t recoverable this way, and neither are output tokens or USD cost, because per-step `output_tokens` is a placeholder.
-###
-
-
-
-Track cache tokens
+### Track cache tokens
The Agent SDK automatically uses [prompt caching](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) to reduce costs on repeated content. You do not need to configure caching yourself. The usage object includes two additional fields for cache tracking:
@@ -331,11 +283,7 @@
Track these separately from `input_tokens` to understand caching savings. In TypeScript, these fields are typed on the [`Usage`](</docs/en/agent-sdk/typescript#usage>) object. In Python, they appear as keys in the [`ResultMessage.usage`](</docs/en/agent-sdk/python#resultmessage>) dict (for example, `message.usage.get("cache_read_input_tokens", 0)`).
-###
-
-
-
-Extend the prompt cache TTL to one hour
+### Extend the prompt cache TTL to one hour
Your own turns fall in the [main conversation TTL bucket](</docs/en/prompt-caching#which-ttl-each-request-gets>), together with the helpers Claude Code runs inline with them. The requests Claude Code makes outside that conversation, such as [subagents](</docs/en/agent-sdk/subagents>), have a [separate TTL control](</docs/en/prompt-caching#choose-the-ttl-yourself>). Cache entries for your own turns use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, Microsoft Foundry, or [Claude Platform on AWS](</docs/en/claude-platform-on-aws>). If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock. Because it sets `CLAUDE_CODE_USE_BEDROCK`, it requires working AWS credentials for [Amazon Bedrock](</docs/en/amazon-bedrock>); without them the query fails.
@@ -380,11 +328,7 @@
Setting `promptCacheTtl` to `1h` keeps the 1-hour cache on the main conversation while you’re drawing on usage credits. For the full precedence order, see [choose the TTL yourself](</docs/en/prompt-caching#choose-the-ttl-yourself>).
-##
-
-
-
-Related documentation
+## Related documentation
* [TypeScript SDK Reference](</docs/en/agent-sdk/typescript>) \- Complete API documentation
* [SDK Overview](</docs/en/agent-sdk/overview>) \- Getting started with the SDK
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -289,7 +289,7 @@
Read output tokens from the result message
-Claude Code builds each assistant message from the usage the API reported when the response began, so the message’s `output_tokens` is only the count the API had reported at `message_start`, before the response was generated. One API response can produce several assistant messages, and every one of them carries that same placeholder. The API reports the real output count at the end of the response, and Claude Code adds it to the result message. Read output tokens from the result’s `usage`, or from `modelUsage` for a per-model breakdown. To watch a response’s output count grow while it streams, set `includePartialMessages`, or `include_partial_messages` in Python, and read `usage` from each `message_delta` stream event, typed as [`SDKPartialAssistantMessage`](</docs/en/agent-sdk/typescript#sdkpartialassistantmessage>) in TypeScript and [`StreamEvent`](</docs/en/agent-sdk/python#streamevent>) in Python.
+Claude Code builds each assistant message from the usage the API reported when the response began, so the message’s `output_tokens` is only the count the API had reported at `message_start`, before the response was generated. One API response can produce several assistant messages, and every one of them carries that same placeholder. The API reports the real output count at the end of the response, and Claude Code adds it to the result message. Read output tokens from the result’s `usage`, or from `modelUsage` for a per-model breakdown.
###
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -6,7 +6,7 @@
* the installed SDK version does not recognize a model
* billing rules apply that the client cannot model
-One billing rule the SDK does model is [data residency pricing](<https://platform.claude.com/docs/en/about-claude/pricing#data-residency-pricing>). When a response’s `usage` reports `inference_geo: "us"`, the SDK multiplies the list price of that response’s tokens by 1.1. Per-request fees such as web search aren’t multiplied. Requires TypeScript Agent SDK v0.3.239 or later, or Python Agent SDK v0.2.144 or later.Use these fields for development insight and approximate budgeting. For authoritative billing, use the [Usage and Cost API](<https://platform.claude.com/docs/en/build-with-claude/usage-cost-api>) or the Usage page in the [Claude Console](<https://platform.claude.com/usage>). Do not bill end users or trigger financial decisions from these fields.
+One billing rule the SDK does model is [data residency pricing](<https://platform.claude.com/docs/en/about-claude/pricing#data-residency-pricing>). When a response’s `usage` reports `inference_geo: "us"`, the SDK multiplies the list price of that response’s tokens by 1.1. Per-request fees such as web search aren’t multiplied. Requires TypeScript Agent SDK v0.3.239 or later, or Python Agent SDK v0.2.144 or later.Use these fields for development insight and approximate budgeting. For authoritative billing, see [Manage costs for your organization](</docs/en/costs#manage-costs-for-your-organization>). Don’t bill end users or trigger financial decisions from these fields.
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -210,7 +210,7 @@
* **Independent calls, with no`resume` or `continue` option**: each result covers only its own call, so add the totals yourself, as the examples below do.
* **Calls that resume the same session** : Claude Code saves the session’s totals to its [transcript](</docs/en/sessions#where-transcripts-are-stored>) when the process exits normally and restores them when a later call resumes or forks the session. Each result already includes the session’s earlier spend. Read the latest result for the session total; summing results double-counts the restored spend. Before v2.1.277, a session that you resumed through the SDK or `claude -p` started its totals at zero, so each call’s results covered only that call.
-In streaming input mode, read each call’s total as described in Track costs in streaming input mode. For a call that ended in a crash, see Recover totals after a session crash. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
+Either way, the combined figure is still a client-side estimate. In streaming input mode, read each call’s total as described in Track costs in streaming input mode. For a call that ended in a crash, see Recover totals after a session crash. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
TypeScript
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -23,7 +23,7 @@
* **`query()` call:** one invocation of the SDK’s `query()` function. A single call can involve multiple steps: Claude responds, uses tools, gets results, and responds again. Each call produces one [`result`](</docs/en/agent-sdk/typescript#sdkresultmessage>) message at the end, except in [streaming input mode](</docs/en/agent-sdk/streaming-vs-single-mode>), where one `query()` call carries multiple user turns and each turn emits its own `result` message.
* **Step:** a single request/response cycle within a `query()` call. Each step produces assistant messages with token usage.
- * **Session:** a series of `query()` calls linked by a session ID (using the `resume` option). Each `query()` call within a session reports its own cost independently.
+ * **Session:** a series of `query()` calls linked by a session ID through the `resume` option. A resumed call’s results report the session’s whole spend, not just that call’s own. See Accumulate costs across multiple calls for how the totals carry over.
The following diagram shows the message stream from a single `query()` call, with token usage reported at each step and the cumulative estimate at the end:
@@ -37,7 +37,7 @@
The result message provides the cumulative estimate
-When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python. If you make multiple `query()` calls, for example in a multi-turn session, each result reflects only the cost of that individual call. If you only need the estimated total, you can ignore the per-step usage and read this single value.In streaming input mode, each turn emits its own result message. See Track costs in streaming input mode for how to read call totals in that mode.
+When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python. If you only need the estimated total, you can ignore the per-step usage and read this single value.If you make multiple independent `query()` calls, each result reflects only the cost of that individual call. A call that resumes a session also counts the session’s earlier spend.In streaming input mode, each turn emits its own result message. See Track costs in streaming input mode for how to read call totals in that mode.
##
@@ -48,7 +48,7 @@
In [streaming input mode](</docs/en/agent-sdk/streaming-vs-single-mode>), one `query()` call carries multiple user turns and each turn emits its own result message. The result fields differ in scope:
* **`usage`** : covers only that turn, and within it only the main agent loop, not any subagents it ran.
- * **`total_cost_usd` and `modelUsage`, or `model_usage` in Python**: carry the running total for the whole call so far.
+ * **`total_cost_usd` and `modelUsage`, or `model_usage` in Python**: carry the running total for the whole call so far, plus any spend restored when the call resumed a session.
In a call where your app never sends `/clear`, `/reset`, or `/new`, read the latest result for call totals rather than summing across results. The running totals start over each time your app sends one of those three commands, and inside a `query()` call nothing else resets them. Three results matter for your accounting:
@@ -56,7 +56,7 @@
* **Every later result** : keeps counting from that reset.
* **The last result before each`/clear`**: holds the total for the turns since the previous reset.
-To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. In Python, the SDK likewise emits a `ConversationResetMessage`. Before Python SDK v0.2.137, the Python iterator dropped that message, so on those versions count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd` (TypeScript) or `max_budget_usd` (Python) is compared against the same running total, so a `/clear` also starts the budget over.
+To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. In Python, the SDK likewise emits a `ConversationResetMessage`. Before Python SDK v0.2.137, the Python iterator dropped that message, so on those versions count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd` (TypeScript) or `max_budget_usd` (Python) counts only the call’s own spend: totals restored from a resumed session don’t count against it, and a `/clear` starts the budget over.
##
@@ -64,7 +64,12 @@
Get the total cost of a query
-The result message, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python, marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. In Python the field is typed as optional, so check that it isn’t `None` before you read it. Success and error results both carry it, though the final result of a session crash may carry it zeroed. If you use sessions to make multiple `query()` calls, each result reflects only the cost of that individual call. In streaming input mode, read call totals as described in Track costs in streaming input mode. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
+The result message, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python, marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. A call that resumes a session also counts the session’s earlier spend. Two caveats apply when you read the value:
+
+ * In Python the field is typed as optional, so check that it isn’t `None` before you read it.
+ * Success and error results both carry it, though the final result of a session crash may carry it zeroed.
+
+In streaming input mode, read call totals as described in Track costs in streaming input mode. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
Field| Subagent activity
---|---
@@ -200,7 +205,12 @@
Accumulate costs across multiple calls
-Each `query()` call returns its own `total_cost_usd`. The SDK doesn’t provide a session-level total, so if your application makes multiple `query()` calls, for example in a multi-turn session or across different users, accumulate the totals yourself. In streaming input mode, read each call’s total as described in Track costs in streaming input mode. For a call that ended in a crash, see Recover totals after a session crash. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
+Each `query()` call returns `total_cost_usd` on its results. How you combine the values depends on whether the calls share a session:
+
+ * **Independent calls, with no`resume` or `continue` option**: each result covers only its own call, so add the totals yourself, as the examples below do.
+ * **Calls that resume the same session** : Claude Code saves the session’s totals to its [transcript](</docs/en/sessions#where-transcripts-are-stored>) when the process exits normally and restores them when a later call resumes or forks the session. Each result already includes the session’s earlier spend. Read the latest result for the session total; summing results double-counts the restored spend. Before v2.1.277, a session that you resumed through the SDK or `claude -p` started its totals at zero, so each call’s results covered only that call.
+
+In streaming input mode, read each call’s total as described in Track costs in streaming input mode. For a call that ended in a crash, see Recover totals after a session crash. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
TypeScript
@@ -302,7 +312,7 @@
When the Claude Code process crashes, it emits a final `error_during_execution` result and exits, in single-shot and streaming input mode alike. That result may carry zeroed `usage`, `total_cost_usd`, and `modelUsage`, so recover the call’s totals from what arrived before it. Step 1 recovers the full totals whenever an earlier result exists; the fallback in step 2 recovers only the main loop’s input and cache tokens.
- 1. Use the result of the turn before the crash. In streaming input mode, it holds the running total since the start of the call or since the last `/clear`. Go to step 2 instead when that result can’t help you:
+ 1. Use the result of the turn before the crash. In streaming input mode, it holds the running total described in Track costs in streaming input mode. Go to step 2 instead when that result can’t help you:
* The call was single-shot, so no earlier result exists.
* The crash happened on the first turn.
* The turn before the crash was the `/clear` itself, so its result covers only the reset.
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -56,7 +56,7 @@
* **Every later result** : keeps counting from that reset.
* **The last result before each`/clear`**: holds the total for the turns since the previous reset.
-To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. In Python, the SDK likewise emits a `ConversationResetMessage`. Before Python SDK v0.2.137, the Python iterator dropped that message, so on those versions count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd`, or `max_budget_usd` in Python, is compared against the same running total, so a `/clear` also starts the budget over.
+To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. In Python, the SDK likewise emits a `ConversationResetMessage`. Before Python SDK v0.2.137, the Python iterator dropped that message, so on those versions count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd` (TypeScript) or `max_budget_usd` (Python) is compared against the same running total, so a `/clear` also starts the budget over.
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -6,7 +6,7 @@
* the installed SDK version does not recognize a model
* billing rules apply that the client cannot model
-Use these fields for development insight and approximate budgeting. For authoritative billing, use the [Usage and Cost API](<https://platform.claude.com/docs/en/build-with-claude/usage-cost-api>) or the Usage page in the [Claude Console](<https://platform.claude.com/usage>). Do not bill end users or trigger financial decisions from these fields.
+One billing rule the SDK does model is [data residency pricing](<https://platform.claude.com/docs/en/about-claude/pricing#data-residency-pricing>). When a response’s `usage` reports `inference_geo: "us"`, the SDK multiplies the list price of that response’s tokens by 1.1. Per-request fees such as web search aren’t multiplied. Requires TypeScript Agent SDK v0.3.239 or later, or Python Agent SDK v0.2.144 or later.Use these fields for development insight and approximate budgeting. For authoritative billing, use the [Usage and Cost API](<https://platform.claude.com/docs/en/build-with-claude/usage-cost-api>) or the Usage page in the [Claude Console](<https://platform.claude.com/usage>). Do not bill end users or trigger financial decisions from these fields.
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -1,6 +1,6 @@
The Claude Agent SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track usage and understand cost reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the [TypeScript SDK reference](</docs/en/agent-sdk/typescript>) and [Python SDK reference](</docs/en/agent-sdk/python>).
-The `total_cost_usd` and `costUSD` fields are client-side estimates, not authoritative billing data. The SDK computes them locally from a price table bundled at build time, so they can drift from what you are actually billed when:
+The `total_cost_usd` and `costUSD` fields are client-side estimates, not authoritative billing data. The SDK computes them locally from a price table bundled at build time, unless a [`modelPricing`](</docs/en/settings-reference#modelpricing>) table is in effect. They can drift from what you are actually billed when:
* pricing changes
* the installed SDK version does not recognize a model
@@ -72,7 +72,7 @@
`total_cost_usd`| Included. Counts subagent requests alongside the top-level loop
`modelUsage` / `model_usage`| Included. Counts subagent requests alongside the top-level loop, broken down by model
-The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
+In [single message input mode](</docs/en/agent-sdk/streaming-vs-single-mode#single-message-input>), when background subagents are still running at the end of the final turn, Claude Code waits for them, up to the cap described in [background tasks at exit](</docs/en/headless#background-tasks-at-exit>), before emitting the result. The result’s `total_cost_usd`, `duration_api_ms`, and `modelUsage`, or `model_usage` in Python, include the work done during that wait. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
TypeScript
@@ -171,7 +171,7 @@
Break down usage per model
-The result message includes [`modelUsage`](</docs/en/agent-sdk/typescript#modelusage>), a map of model name to per-model token counts and cost. This is useful when you run multiple models (for example, Haiku for subagents and Opus for the main agent) and want to see where tokens are going. The following example runs a query and prints the cost and token breakdown for each model used:
+The result message includes [`modelUsage`](</docs/en/agent-sdk/typescript#modelusage>), a map of model name to per-model token counts and cost. This is useful when you run multiple models (for example, Haiku for subagents and Opus for the main agent) and want to see where tokens are going. Each entry’s `costBasis` says which price table priced that model’s latest request: `list` for list price, `managed` for a [`modelPricing`](</docs/en/settings-reference#modelpricing>) table, or `unknown` when neither matched the model ID. The field requires Claude Code v2.1.246 or later. The following example runs a query and prints the cost and token breakdown for each model used:
import { query } from "@anthropic-ai/claude-agent-sdk";
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -327,7 +327,7 @@
Extend the prompt cache TTL to one hour
-Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, Microsoft Foundry, or [Claude Platform on AWS](</docs/en/claude-platform-on-aws>). If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock. Because it sets `CLAUDE_CODE_USE_BEDROCK`, it requires working AWS credentials for [Amazon Bedrock](</docs/en/amazon-bedrock>); without them the query fails.
+Your own turns fall in the [main conversation TTL bucket](</docs/en/prompt-caching#which-ttl-each-request-gets>), together with the helpers Claude Code runs inline with them. The requests Claude Code makes outside that conversation, such as [subagents](</docs/en/agent-sdk/subagents>), have a [separate TTL control](</docs/en/prompt-caching#choose-the-ttl-yourself>). Cache entries for your own turns use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, Microsoft Foundry, or [Claude Platform on AWS](</docs/en/claude-platform-on-aws>). If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock. Because it sets `CLAUDE_CODE_USE_BEDROCK`, it requires working AWS credentials for [Amazon Bedrock](</docs/en/amazon-bedrock>); without them the query fails.
Python
@@ -363,7 +363,12 @@
console.log(message);
}
-Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. Claude subscription users within included usage receive the 1-hour TTL automatically without setting this variable. When you’re drawing on [usage credits](<https://support.claude.com/en/articles/12429409-extra-usage-for-paid-claude-plans>), the SDK drops to the 5-minute TTL unless you set `ENABLE_PROMPT_CACHING_1H`.
+Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. On a Claude subscription within your plan’s included usage, you get the 1-hour TTL on your own turns, and on some of the helper requests Claude Code makes beside them, without setting this variable, and Claude Code drops those turns to the 5-minute TTL once you’re drawing on [usage credits](<https://support.claude.com/en/articles/12429409-extra-usage-for-paid-claude-plans>). `ENABLE_PROMPT_CACHING_1H` asks for the 1-hour TTL on every request in both buckets. To choose a TTL for each bucket separately, use these controls instead. Each takes `5m` or `1h` and takes precedence over `ENABLE_PROMPT_CACHING_1H`:
+
+ * Main conversation: the `CLAUDE_CODE_PROMPT_CACHE_TTL` [environment variable](</docs/en/env-vars>), or the [`promptCacheTtl`](</docs/en/settings-reference#promptcachettl>) setting
+ * Everything else: the `CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL` environment variable, or the [`subagentPromptCacheTtl`](</docs/en/settings-reference#subagentpromptcachettl>) setting
+
+Setting `promptCacheTtl` to `1h` keeps the 1-hour cache on the main conversation while you’re drawing on usage credits. For the full precedence order, see [choose the TTL yourself](</docs/en/prompt-caching#choose-the-ttl-yourself>).
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -56,7 +56,7 @@
* **Every later result** : keeps counting from that reset.
* **The last result before each`/clear`**: holds the total for the turns since the previous reset.
-To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. The Python SDK drops that message before it reaches your app, so in Python, count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd`, or `max_budget_usd` in Python, is compared against the same running total, so a `/clear` also starts the budget over.
+To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. In Python, the SDK likewise emits a `ConversationResetMessage`. Before Python SDK v0.2.137, the Python iterator dropped that message, so on those versions count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd`, or `max_budget_usd` in Python, is compared against the same running total, so a `/clear` also starts the budget over.
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -109,6 +109,8 @@
print(f"Session ended with an error: {error}")
asyncio.run(main())
+
+To bound how much subagents can add to `total_cost_usd`, set the [depth, concurrency, and spend limits](</docs/en/agent-sdk/subagents#cap-subagent-depth-concurrency-and-spend>) on the query.
##
Show diff (174 lines)
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -17,11 +17,11 @@
The TypeScript and Python SDKs expose the same usage data with different field names:
* **TypeScript** provides per-step token breakdowns on each assistant message (`message.message.id`, `message.message.usage`), per-model cost via `modelUsage` on the result message, and a cumulative total on the result message.
- * **Python** provides per-step token breakdowns on each assistant message (`message.usage`, `message.message_id`), per-model cost via `model_usage` on the result message, and the accumulated total on the result message (`total_cost_usd` and `usage` dict).
+ * **Python** provides per-step token breakdowns on each assistant message as `message.usage` and `message.message_id`, per-model cost via `model_usage` on the result message, and the cumulative total on the result message as `total_cost_usd`.
Both SDKs use the same underlying cost model and expose the same granularity. The difference is in field naming and where per-step usage is nested. Cost tracking depends on understanding how the SDK scopes usage data:
- * **`query()` call:** one invocation of the SDK’s `query()` function. A single call can involve multiple steps (Claude responds, uses tools, gets results, responds again). Each call produces one [`result`](</docs/en/agent-sdk/typescript#sdkresultmessage>) message at the end.
+ * **`query()` call:** one invocation of the SDK’s `query()` function. A single call can involve multiple steps: Claude responds, uses tools, gets results, and responds again. Each call produces one [`result`](</docs/en/agent-sdk/typescript#sdkresultmessage>) message at the end, except in [streaming input mode](</docs/en/agent-sdk/streaming-vs-single-mode>), where one `query()` call carries multiple user turns and each turn emits its own `result` message.
* **Step:** a single request/response cycle within a `query()` call. Each step produces assistant messages with token usage.
* **Session:** a series of `query()` calls linked by a session ID (using the `resume` option). Each `query()` call within a session reports its own cost independently.
@@ -37,7 +37,26 @@
The result message provides the cumulative estimate
-When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`. This is available in both TypeScript ([`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>)) and Python ([`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>)). If you make multiple `query()` calls (for example, in a multi-turn session), each result only reflects the cost of that individual call. If you only need the estimated total, you can ignore the per-step usage and read this single value.
+When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python. If you make multiple `query()` calls, for example in a multi-turn session, each result reflects only the cost of that individual call. If you only need the estimated total, you can ignore the per-step usage and read this single value.In streaming input mode, each turn emits its own result message. See Track costs in streaming input mode for how to read call totals in that mode.
+
+##
+
+
+
+Track costs in streaming input mode
+
+In [streaming input mode](</docs/en/agent-sdk/streaming-vs-single-mode>), one `query()` call carries multiple user turns and each turn emits its own result message. The result fields differ in scope:
+
+ * **`usage`** : covers only that turn, and within it only the main agent loop, not any subagents it ran.
+ * **`total_cost_usd` and `modelUsage`, or `model_usage` in Python**: carry the running total for the whole call so far.
+
+In a call where your app never sends `/clear`, `/reset`, or `/new`, read the latest result for call totals rather than summing across results. The running totals start over each time your app sends one of those three commands, and inside a `query()` call nothing else resets them. Three results matter for your accounting:
+
+ * **The`/clear` turn’s own result**: covers only what has run since the reset, and carries a new `session_id`.
+ * **Every later result** : keeps counting from that reset.
+ * **The last result before each`/clear`**: holds the total for the turns since the previous reset.
+
+To total the whole call, add the last result from before each `/clear` to the call’s final result. Every other result, including the `/clear` turn’s own, is superseded by a later one. In TypeScript, the SDK also emits an [`SDKConversationResetMessage`](</docs/en/agent-sdk/typescript#sdkconversationresetmessage>) at each reset, so you can detect resets from the stream. The Python SDK drops that message before it reaches your app, so in Python, count the resets yourself from the `/clear` turns your app sends. `maxBudgetUsd`, or `max_budget_usd` in Python, is compared against the same running total, so a `/clear` also starts the budget over.
##
@@ -45,7 +64,7 @@
Get the total cost of a query
-The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdkresultmessage>), [Python](</docs/en/agent-sdk/python#resultmessage>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results, though in Python the field is typed as optional and may be `None` on some error paths. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
+The result message, typed as [`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>) in TypeScript and [`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>) in Python, marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. In Python the field is typed as optional, so check that it isn’t `None` before you read it. Success and error results both carry it, though the final result of a session crash may carry it zeroed. If you use sessions to make multiple `query()` calls, each result reflects only the cost of that individual call. In streaming input mode, read call totals as described in Track costs in streaming input mode. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
Field| Subagent activity
---|---
@@ -107,38 +126,42 @@
Each assistant message contains a nested `BetaMessage` (accessed via `message.message`) with an `id` and `usage` object with token counts. When Claude uses tools in parallel, multiple messages share the same `id` with identical usage data. Track which IDs you’ve already counted and skip duplicates to avoid inflated totals.
-Parallel tool calls produce multiple assistant messages whose nested `BetaMessage` shares the same `id` and identical usage. Always deduplicate by ID to get accurate per-step token counts.
-
-The following example accumulates input and output tokens across all steps, counting each unique message ID only once:
+The deduplicated per-step values are accurate for input and cache tokens. Per-step `output_tokens` is a placeholder, so read output tokens from the result message.
+
+The following example accumulates input tokens across all steps, counting each unique main-loop message ID only once and skipping subagent messages, and reads the output total from the result message, which covers the main loop:
import { query } from "@anthropic-ai/claude-agent-sdk";
const seenIds = new Set<string>();
let totalInputTokens = 0;
- let totalOutputTokens = 0;
+ let resultOutputTokens = 0;
try {
for await (const message of query({ prompt: "Summarize this project" })) {
- if (message.type === "assistant") {
+ if (message.type === "assistant" && !message.parent_tool_use_id) {
const msgId = message.message.id;
// Parallel tool calls share the same ID, only count once
if (!seenIds.has(msgId)) {
seenIds.add(msgId);
totalInputTokens += message.message.usage.input_tokens;
- totalOutputTokens += message.message.usage.output_tokens;
}
+ }
+ if (message.type === "result") {
+ // Per-step output_tokens is a placeholder; the result message
+ // carries the accumulated output total.
+ resultOutputTokens = message.usage.output_tokens;
}
}
} catch (error) {
// A single-shot query() throws after yielding an error result, so the
- // totals below still reflect the steps that ran before the failure.
+ // input total below still reflects the steps that ran before the failure.
console.error(`Session ended with an error: ${error}`);
}
console.log(`Steps: ${seenIds.size}`);
console.log(`Input tokens: ${totalInputTokens}`);
- console.log(`Output tokens: ${totalOutputTokens}`);
+ console.log(`Output tokens: ${resultOutputTokens}`);
###
@@ -175,7 +198,7 @@
Accumulate costs across multiple calls
-Each `query()` call returns its own `total_cost_usd`. The SDK does not provide a session-level total, so if your application makes multiple `query()` calls (for example, in a multi-turn session or across different users), accumulate the totals yourself. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
+Each `query()` call returns its own `total_cost_usd`. The SDK doesn’t provide a session-level total, so if your application makes multiple `query()` calls, for example in a multi-turn session or across different users, accumulate the totals yourself. In streaming input mode, read each call’s total as described in Track costs in streaming input mode. For a call that ended in a crash, see Recover totals after a session crash. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
TypeScript
@@ -244,21 +267,17 @@
-Handle errors, caching, and token discrepancies
-
-For accurate cost tracking, account for failed conversations, cache token pricing, and occasional reporting inconsistencies.
-
-###
-
-
-
-Resolve output token discrepancies
-
-In rare cases, you might observe different `output_tokens` values for messages with the same ID. When this occurs:
-
- 1. **Use the highest value:** the final message in a group typically contains the accurate total.
- 2. **Prefer the result message:** the `total_cost_usd` in the result message reflects the SDK’s accumulated estimate across all steps, so it is more reliable than summing per-step values yourself. It is still an estimate and may differ from your actual bill.
- 3. **Report inconsistencies:** file issues at the [Claude Code GitHub repository](<https://github.com/anthropics/claude-code/issues>).
+Handle errors, caching, and output token counts
+
+For accurate cost tracking, account for the placeholder output count on assistant messages, the tokens a failed conversation consumed, and cache token pricing.
+
+###
+
+
+
+Read output tokens from the result message
+
+Claude Code builds each assistant message from the usage the API reported when the response began, so the message’s `output_tokens` is only the count the API had reported at `message_start`, before the response was generated. One API response can produce several assistant messages, and every one of them carries that same placeholder. The API reports the real output count at the end of the response, and Claude Code adds it to the result message. Read output tokens from the result’s `usage`, or from `modelUsage` for a per-model breakdown. To watch a response’s output count grow while it streams, set `includePartialMessages`, or `include_partial_messages` in Python, and read `usage` from each `message_delta` stream event, typed as [`SDKPartialAssistantMessage`](</docs/en/agent-sdk/typescript#sdkpartialassistantmessage>) in TypeScript and [`StreamEvent`](</docs/en/agent-sdk/python#streamevent>) in Python.
###
@@ -266,7 +285,26 @@
Track costs on failed conversations
-Both success and error result messages include `usage` and `total_cost_usd`; in Python both fields are typed as optional and may be `None` on some error paths. If a conversation fails mid-way, you still consumed tokens up to the point of failure. Always read cost data from the result message regardless of its `subtype`.
+Both success and error result messages include `usage` and `total_cost_usd`; in Python both fields are typed as optional, so check that they aren’t `None` before you read them. If a conversation fails midway, you still consumed tokens up to the point of failure. Read cost data from every result message, whether its `subtype` is `success` or one of the error subtypes. On some error results, `usage` reports less than the call spent:
+
+ * **`error_during_execution` after a session crash**: every cost field may be zeroed.
+ * **`error_max_budget_usd`** : `usage` leaves out the response that crossed the budget, while `total_cost_usd` and `modelUsage` include it.
+
+Where you have the choice, account from `total_cost_usd` or `modelUsage` rather than `usage`.
+
+###
+
+
+
+Recover totals after a session crash
+
+When the Claude Code process crashes, it emits a final `error_during_execution` result and exits, in single-shot and streaming input mode alike. That result may carry zeroed `usage`, `total_cost_usd`, and `modelUsage`, so recover the call’s totals from what arrived before it. Step 1 recovers the full totals whenever an earlier result exists; the fallback in step 2 recovers only the main loop’s input and cache tokens.
+
+ 1. Use the result of the turn before the crash. In streaming input mode, it holds the running total since the start of the call or since the last `/clear`. Go to step 2 instead when that result can’t help you:
+ * The call was single-shot, so no earlier result exists.
+ * The crash happened on the first turn.
+ * The turn before the crash was the `/clear` itself, so its result covers only the reset.
+ 2. Sum the `usage` on the assistant messages instead, counting each API response once, as the Track per-step usage example does. In single-shot mode, sum all of them; in streaming input mode, sum the ones that arrived after the last result. This gives you the main loop’s input and cache tokens. Subagent usage isn’t recoverable this way, and neither are output tokens or USD cost, because per-step `output_tokens` is a placeholder.
###
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -45,7 +45,7 @@
Get the total cost of a query
-The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdkresultmessage>), [Python](</docs/en/agent-sdk/python#resultmessage>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
+The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdkresultmessage>), [Python](</docs/en/agent-sdk/python#resultmessage>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results, though in Python the field is typed as optional and may be `None` on some error paths. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
Field| Subagent activity
---|---
@@ -85,9 +85,8 @@
print(f"Total cost: ${message.total_cost_usd or 0}")
except Exception as error:
# A single-shot query() raises after yielding an error result. If the
- # failure was an error result, it still carried total_cost_usd and the
- # branch above has already run; connection or process failures yield
- # no result message.
+ # failure was an error result, the branch above has already run;
+ # connection or process failures yield no result message.
print(f"Session ended with an error: {error}")
asyncio.run(main())
@@ -267,7 +266,7 @@
Track costs on failed conversations
-Both success and error result messages include `usage` and `total_cost_usd`. If a conversation fails mid-way, you still consumed tokens up to the point of failure. Always read cost data from the result message regardless of its `subtype`.
+Both success and error result messages include `usage` and `total_cost_usd`; in Python both fields are typed as optional and may be `None` on some error paths. If a conversation fails mid-way, you still consumed tokens up to the point of failure. Always read cost data from the result message regardless of its `subtype`.
###
@@ -288,7 +287,7 @@
Extend the prompt cache TTL to one hour
-Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, or Microsoft Foundry. If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock. Because it sets `CLAUDE_CODE_USE_BEDROCK`, it requires working AWS credentials for [Amazon Bedrock](</docs/en/amazon-bedrock>); without them the query fails.
+Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, Microsoft Foundry, or [Claude Platform on AWS](</docs/en/claude-platform-on-aws>). If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock. Because it sets `CLAUDE_CODE_USE_BEDROCK`, it requires working AWS credentials for [Amazon Bedrock](</docs/en/amazon-bedrock>); without them the query fails.
Python
@@ -324,7 +323,7 @@
console.log(message);
}
-Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. Claude subscription users already receive 1-hour TTL automatically and do not need to set this variable.
+Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. Claude subscription users within included usage receive the 1-hour TTL automatically without setting this variable. When you’re drawing on [usage credits](<https://support.claude.com/en/articles/12429409-extra-usage-for-paid-claude-plans>), the SDK drops to the 5-minute TTL unless you set `ENABLE_PROMPT_CACHING_1H`.
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -288,7 +288,7 @@
Extend the prompt cache TTL to one hour
-Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, or Microsoft Foundry. If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock:
+Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, or Microsoft Foundry. If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock. Because it sets `CLAUDE_CODE_USE_BEDROCK`, it requires working AWS credentials for [Amazon Bedrock](</docs/en/amazon-bedrock>); without them the query fails.
Python
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -45,7 +45,15 @@
Get the total cost of a query
-The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdkresultmessage>), [Python](</docs/en/agent-sdk/python#resultmessage>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
+The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdkresultmessage>), [Python](</docs/en/agent-sdk/python#resultmessage>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The three result-level fields differ in what they count when the agent spawns [subagents](</docs/en/agent-sdk/subagents>). Use `modelUsage`, or `model_usage` in Python, for whole-tree token accounting; the `usage` field undercounts as soon as nesting occurs.
+
+Field| Subagent activity
+---|---
+`usage`| Excluded. Counts only the top-level agent loop, so tokens consumed inside subagents are not added
+`total_cost_usd`| Included. Counts subagent requests alongside the top-level loop
+`modelUsage` / `model_usage`| Included. Counts subagent requests alongside the top-level loop, broken down by model
+
+The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
TypeScript
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -66,6 +66,23 @@
// no result message.
console.error(`Session ended with an error: ${error}`);
}
+
+ from claude_agent_sdk import query, ResultMessage
+ import asyncio
+
+ async def main():
+ try:
+ async for message in query(prompt="Summarize this project"):
+ if isinstance(message, ResultMessage):
+ print(f"Total cost: ${message.total_cost_usd or 0}")
+ except Exception as error:
+ # A single-shot query() raises after yielding an error result. If the
+ # failure was an error result, it still carried total_cost_usd and the
+ # branch above has already run; connection or process failures yield
+ # no result message.
+ print(f"Session ended with an error: {error}")
+
+ asyncio.run(main())
##
@@ -186,6 +203,36 @@
console.log(`Total spend: $${totalSpend.toFixed(4)}`);
+ from claude_agent_sdk import query, ResultMessage
+ import asyncio
+
+ async def main():
+ # Track cumulative cost across multiple query() calls
+ total_spend = 0.0
+
+ prompts = [
+ "Read the files in src/ and summarize the architecture",
+ "List all exported functions in src/auth.ts",
+ ]
+
+ for prompt in prompts:
+ try:
+ async for message in query(prompt=prompt):
+ if isinstance(message, ResultMessage):
+ cost = message.total_cost_usd or 0
+ total_spend += cost
+ print(f"This call: ${cost}")
+ except Exception as error:
+ # A single-shot query() raises after yielding an error result. If
+ # the failure was an error result, this call's cost was already
+ # counted; connection or process failures yield no result message.
+ # Continue with the next prompt.
+ print(f"Call failed: {error}")
+
+ print(f"Total spend: ${total_spend:.4f}")
+
+ asyncio.run(main())
+
##
@@ -255,6 +302,20 @@
asyncio.run(main())
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ const options = {
+ env: {
+ ...process.env,
+ CLAUDE_CODE_USE_BEDROCK: "1",
+ ENABLE_PROMPT_CACHING_1H: "1",
+ },
+ };
+
+ for await (const message of query({ prompt: "Summarize this project", options })) {
+ console.log(message);
+ }
+
Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. Claude subscription users already receive 1-hour TTL automatically and do not need to set this variable.
##
Show diff (128 lines)
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -53,10 +53,18 @@
import { query } from "@anthropic-ai/claude-agent-sdk";
- for await (const message of query({ prompt: "Summarize this project" })) {
- if (message.type === "result") {
- console.log(`Total cost: $${message.total_cost_usd}`);
- }
+ try {
+ for await (const message of query({ prompt: "Summarize this project" })) {
+ if (message.type === "result") {
+ console.log(`Total cost: $${message.total_cost_usd}`);
+ }
+ }
+ } catch (error) {
+ // A single-shot query() throws after yielding an error result. If the
+ // failure was an error result, it still carried total_cost_usd and the
+ // branch above has already run; connection or process failures yield
+ // no result message.
+ console.error(`Session ended with an error: ${error}`);
}
##
@@ -85,17 +93,23 @@
let totalInputTokens = 0;
let totalOutputTokens = 0;
- for await (const message of query({ prompt: "Summarize this project" })) {
- if (message.type === "assistant") {
- const msgId = message.message.id;
-
- // Parallel tool calls share the same ID, only count once
- if (!seenIds.has(msgId)) {
- seenIds.add(msgId);
- totalInputTokens += message.message.usage.input_tokens;
- totalOutputTokens += message.message.usage.output_tokens;
- }
- }
+ try {
+ for await (const message of query({ prompt: "Summarize this project" })) {
+ if (message.type === "assistant") {
+ const msgId = message.message.id;
+
+ // Parallel tool calls share the same ID, only count once
+ if (!seenIds.has(msgId)) {
+ seenIds.add(msgId);
+ totalInputTokens += message.message.usage.input_tokens;
+ totalOutputTokens += message.message.usage.output_tokens;
+ }
+ }
+ }
+ } catch (error) {
+ // A single-shot query() throws after yielding an error result, so the
+ // totals below still reflect the steps that ran before the failure.
+ console.error(`Session ended with an error: ${error}`);
}
console.log(`Steps: ${seenIds.size}`);
@@ -112,16 +126,23 @@
import { query } from "@anthropic-ai/claude-agent-sdk";
- for await (const message of query({ prompt: "Summarize this project" })) {
- if (message.type !== "result") continue;
-
- for (const [modelName, usage] of Object.entries(message.modelUsage)) {
- console.log(`${modelName}: $${usage.costUSD.toFixed(4)}`);
- console.log(` Input tokens: ${usage.inputTokens}`);
- console.log(` Output tokens: ${usage.outputTokens}`);
- console.log(` Cache read: ${usage.cacheReadInputTokens}`);
- console.log(` Cache creation: ${usage.cacheCreationInputTokens}`);
- }
+ try {
+ for await (const message of query({ prompt: "Summarize this project" })) {
+ if (message.type !== "result") continue;
+
+ for (const [modelName, usage] of Object.entries(message.modelUsage)) {
+ console.log(`${modelName}: $${usage.costUSD.toFixed(4)}`);
+ console.log(` Input tokens: ${usage.inputTokens}`);
+ console.log(` Output tokens: ${usage.outputTokens}`);
+ console.log(` Cache read: ${usage.cacheReadInputTokens}`);
+ console.log(` Cache creation: ${usage.cacheCreationInputTokens}`);
+ }
+ }
+ } catch (error) {
+ // A single-shot query() throws after yielding an error result. If the
+ // failure was an error result, the per-model breakdown above has already
+ // printed; connection or process failures yield no result message.
+ console.error(`Session ended with an error: ${error}`);
}
##
@@ -147,11 +168,19 @@
];
for (const prompt of prompts) {
- for await (const message of query({ prompt })) {
- if (message.type === "result") {
- totalSpend += message.total_cost_usd;
- console.log(`This call: $${message.total_cost_usd}`);
- }
+ try {
+ for await (const message of query({ prompt })) {
+ if (message.type === "result") {
+ totalSpend += message.total_cost_usd;
+ console.log(`This call: $${message.total_cost_usd}`);
+ }
+ }
+ } catch (error) {
+ // A single-shot query() throws after yielding an error result. If the
+ // failure was an error result, this call's cost was already counted;
+ // connection or process failures yield no result message. Continue
+ // with the next prompt.
+ console.error(`Call failed: ${error}`);
}
}
@@ -204,7 +233,7 @@
Extend the prompt cache TTL to one hour
-Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud Vertex AI, or Microsoft Foundry. If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Bedrock:
+Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud’s Agent Platform, or Microsoft Foundry. If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Amazon Bedrock:
Python
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -210,12 +210,21 @@
TypeScript
- options = ClaudeAgentOptions(
- env={
- "CLAUDE_CODE_USE_BEDROCK": "1",
- "ENABLE_PROMPT_CACHING_1H": "1",
- },
- )
+ from claude_agent_sdk import ClaudeAgentOptions, query
+ import asyncio
+
+ async def main():
+ options = ClaudeAgentOptions(
+ env={
+ "CLAUDE_CODE_USE_BEDROCK": "1",
+ "ENABLE_PROMPT_CACHING_1H": "1",
+ },
+ )
+
+ async for message in query(prompt="Summarize this project", options=options):
+ print(message)
+
+ asyncio.run(main())
Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. Claude subscription users already receive 1-hour TTL automatically and do not need to set this variable.
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -1,9 +1,3 @@
-> ## Documentation Index
->
-> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt>
->
-> Use this file to discover all available pages before exploring further.
-
The Claude Agent SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track usage and understand cost reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the [TypeScript SDK reference](</docs/en/agent-sdk/typescript>) and [Python SDK reference](</docs/en/agent-sdk/python>).
The `total_cost_usd` and `costUSD` fields are client-side estimates, not authoritative billing data. The SDK computes them locally from a price table bundled at build time, so they can drift from what you are actually billed when:
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -27,7 +27,7 @@
Both SDKs use the same underlying cost model and expose the same granularity. The difference is in field naming and where per-step usage is nested. Cost tracking depends on understanding how the SDK scopes usage data:
- * **`query()` call:** one invocation of the SDK’s `query()` function. A single call can involve multiple steps (Claude responds, uses tools, gets results, responds again). Each call produces one [`result`](</docs/en/agent-sdk/typescript#sdk-result-message>) message at the end.
+ * **`query()` call:** one invocation of the SDK’s `query()` function. A single call can involve multiple steps (Claude responds, uses tools, gets results, responds again). Each call produces one [`result`](</docs/en/agent-sdk/typescript#sdkresultmessage>) message at the end.
* **Step:** a single request/response cycle within a `query()` call. Each step produces assistant messages with token usage.
* **Session:** a series of `query()` calls linked by a session ID (using the `resume` option). Each `query()` call within a session reports its own cost independently.
@@ -43,7 +43,7 @@
The result message provides the cumulative estimate
-When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`. This is available in both TypeScript ([`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdk-result-message>)) and Python ([`ResultMessage`](</docs/en/agent-sdk/python#result-message>)). If you make multiple `query()` calls (for example, in a multi-turn session), each result only reflects the cost of that individual call. If you only need the estimated total, you can ignore the per-step usage and read this single value.
+When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`. This is available in both TypeScript ([`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdkresultmessage>)) and Python ([`ResultMessage`](</docs/en/agent-sdk/python#resultmessage>)). If you make multiple `query()` calls (for example, in a multi-turn session), each result only reflects the cost of that individual call. If you only need the estimated total, you can ignore the per-step usage and read this single value.
##
@@ -51,7 +51,7 @@
Get the total cost of a query
-The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdk-result-message>), [Python](</docs/en/agent-sdk/python#result-message>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
+The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdkresultmessage>), [Python](</docs/en/agent-sdk/python#resultmessage>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
TypeScript
@@ -71,7 +71,7 @@
Track per-step and per-model usage
-The examples in this section use TypeScript field names. In Python, the equivalent fields are [`AssistantMessage.usage`](</docs/en/agent-sdk/python#assistant-message>) and `AssistantMessage.message_id` for per-step usage, and [`ResultMessage.model_usage`](</docs/en/agent-sdk/python#result-message>) for per-model breakdowns.
+The examples in this section use TypeScript field names. In Python, the equivalent fields are [`AssistantMessage.usage`](</docs/en/agent-sdk/python#assistantmessage>) and `AssistantMessage.message_id` for per-step usage, and [`ResultMessage.model_usage`](</docs/en/agent-sdk/python#resultmessage>) for per-model breakdowns.
###
@@ -114,7 +114,7 @@
Break down usage per model
-The result message includes [`modelUsage`](</docs/en/agent-sdk/typescript#model-usage>), a map of model name to per-model token counts and cost. This is useful when you run multiple models (for example, Haiku for subagents and Opus for the main agent) and want to see where tokens are going. The following example runs a query and prints the cost and token breakdown for each model used:
+The result message includes [`modelUsage`](</docs/en/agent-sdk/typescript#modelusage>), a map of model name to per-model token counts and cost. This is useful when you run multiple models (for example, Haiku for subagents and Opus for the main agent) and want to see where tokens are going. The following example runs a query and prints the cost and token breakdown for each model used:
import { query } from "@anthropic-ai/claude-agent-sdk";
@@ -202,7 +202,7 @@
* `cache_creation_input_tokens`: tokens used to create new cache entries (charged at a higher rate than standard input tokens).
* `cache_read_input_tokens`: tokens read from existing cache entries (charged at a reduced rate).
-Track these separately from `input_tokens` to understand caching savings. In TypeScript, these fields are typed on the [`Usage`](</docs/en/agent-sdk/typescript#usage>) object. In Python, they appear as keys in the [`ResultMessage.usage`](</docs/en/agent-sdk/python#result-message>) dict (for example, `message.usage.get("cache_read_input_tokens", 0)`).
+Track these separately from `input_tokens` to understand caching savings. In TypeScript, these fields are typed on the [`Usage`](</docs/en/agent-sdk/typescript#usage>) object. In Python, they appear as keys in the [`ResultMessage.usage`](</docs/en/agent-sdk/python#resultmessage>) dict (for example, `message.usage.get("cache_read_input_tokens", 0)`).
###
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -1,3 +1,9 @@
+> ## Documentation Index
+>
+> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt>
+>
+> Use this file to discover all available pages before exploring further.
+
The Claude Agent SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track usage and understand cost reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the [TypeScript SDK reference](</docs/en/agent-sdk/typescript>) and [Python SDK reference](</docs/en/agent-sdk/python>).
The `total_cost_usd` and `costUSD` fields are client-side estimates, not authoritative billing data. The SDK computes them locally from a price table bundled at build time, so they can drift from what you are actually billed when:
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -198,6 +198,27 @@
Track these separately from `input_tokens` to understand caching savings. In TypeScript, these fields are typed on the [`Usage`](</docs/en/agent-sdk/typescript#usage>) object. In Python, they appear as keys in the [`ResultMessage.usage`](</docs/en/agent-sdk/python#result-message>) dict (for example, `message.usage.get("cache_read_input_tokens", 0)`).
+###
+
+
+
+Extend the prompt cache TTL to one hour
+
+Cache entries written by the SDK use a 5-minute TTL by default when you authenticate with an API key or run on Amazon Bedrock, Google Cloud Vertex AI, or Microsoft Foundry. If your workload runs many short sessions against the same system prompt and context with gaps longer than 5 minutes between them, the cache expires between sessions and each new session pays full input price. To request a 1-hour TTL on cache writes, set the [`ENABLE_PROMPT_CACHING_1H`](</docs/en/env-vars>) environment variable. You can export it in your shell or container environment, or pass it through `options.env`. The following example enables 1-hour TTL for an agent running on Bedrock:
+
+Python
+
+TypeScript
+
+ options = ClaudeAgentOptions(
+ env={
+ "CLAUDE_CODE_USE_BEDROCK": "1",
+ "ENABLE_PROMPT_CACHING_1H": "1",
+ },
+ )
+
+Cache writes with a 1-hour TTL are billed at a higher rate than 5-minute writes, so enabling this trades higher write cost for more cache reads. See [prompt caching pricing](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) for details. Claude subscription users already receive 1-hour TTL automatically and do not need to set this variable.
+
##
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -45,7 +45,7 @@
Get the total cost of a query
-The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdk-result-message>), [Python](</docs/en/agent-sdk/python#result-message>)) is the last message in every `query()` call. It includes `total_cost_usd`, the cumulative cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
+The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdk-result-message>), [Python](</docs/en/agent-sdk/python#result-message>)) marks the end of the agent loop for a `query()` call. It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
TypeScript
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -1,4 +1,12 @@
-The Claude Agent SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track costs and understand usage reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the [TypeScript SDK reference](</docs/en/agent-sdk/typescript>) and [Python SDK reference](</docs/en/agent-sdk/python>).
+The Claude Agent SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track usage and understand cost reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the [TypeScript SDK reference](</docs/en/agent-sdk/typescript>) and [Python SDK reference](</docs/en/agent-sdk/python>).
+
+The `total_cost_usd` and `costUSD` fields are client-side estimates, not authoritative billing data. The SDK computes them locally from a price table bundled at build time, so they can drift from what you are actually billed when:
+
+ * pricing changes
+ * the installed SDK version does not recognize a model
+ * billing rules apply that the client cannot model
+
+Use these fields for development insight and approximate budgeting. For authoritative billing, use the [Usage and Cost API](<https://platform.claude.com/docs/en/build-with-claude/usage-cost-api>) or the Usage page in the [Claude Console](<https://platform.claude.com/usage>). Do not bill end users or trigger financial decisions from these fields.
##
@@ -17,7 +25,7 @@
* **Step:** a single request/response cycle within a `query()` call. Each step produces assistant messages with token usage.
* **Session:** a series of `query()` calls linked by a session ID (using the `resume` option). Each `query()` call within a session reports its own cost independently.
-The following diagram shows the message stream from a single `query()` call, with token usage reported at each step and the authoritative total at the end:
+The following diagram shows the message stream from a single `query()` call, with token usage reported at each step and the cumulative estimate at the end:
1
@@ -27,9 +35,9 @@
2
-The result message provides the authoritative total
-
-When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`. This is available in both TypeScript ([`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdk-result-message>)) and Python ([`ResultMessage`](</docs/en/agent-sdk/python#result-message>)). If you make multiple `query()` calls (for example, in a multi-turn session), each result only reflects the cost of that individual call. If you only need the total cost, you can ignore the per-step usage and read this single value.
+The result message provides the cumulative estimate
+
+When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`. This is available in both TypeScript ([`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdk-result-message>)) and Python ([`ResultMessage`](</docs/en/agent-sdk/python#result-message>)). If you make multiple `query()` calls (for example, in a multi-turn session), each result only reflects the cost of that individual call. If you only need the estimated total, you can ignore the per-step usage and read this single value.
##
@@ -166,7 +174,7 @@
In rare cases, you might observe different `output_tokens` values for messages with the same ID. When this occurs:
1. **Use the highest value:** the final message in a group typically contains the accurate total.
- 2. **Verify against total cost:** the `total_cost_usd` in the result message is authoritative.
+ 2. **Prefer the result message:** the `total_cost_usd` in the result message reflects the SDK’s accumulated estimate across all steps, so it is more reliable than summing per-step values yourself. It is still an estimate and may differ from your actual bill.
3. **Report inconsistencies:** file issues at the [Claude Code GitHub repository](<https://github.com/anthropics/claude-code/issues>).
###
Show diff (204 lines)
--- a/cost-tracking.md
+++ b/cost-tracking.md
@@ -0,0 +1,201 @@
+The Claude Agent SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track costs and understand usage reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the [TypeScript SDK reference](</docs/en/agent-sdk/typescript>) and [Python SDK reference](</docs/en/agent-sdk/python>).
+
+##
+
+
+
+Understand token usage
+
+The TypeScript and Python SDKs expose the same usage data with different field names:
+
+ * **TypeScript** provides per-step token breakdowns on each assistant message (`message.message.id`, `message.message.usage`), per-model cost via `modelUsage` on the result message, and a cumulative total on the result message.
+ * **Python** provides per-step token breakdowns on each assistant message (`message.usage`, `message.message_id`), per-model cost via `model_usage` on the result message, and the accumulated total on the result message (`total_cost_usd` and `usage` dict).
+
+Both SDKs use the same underlying cost model and expose the same granularity. The difference is in field naming and where per-step usage is nested. Cost tracking depends on understanding how the SDK scopes usage data:
+
+ * **`query()` call:** one invocation of the SDK’s `query()` function. A single call can involve multiple steps (Claude responds, uses tools, gets results, responds again). Each call produces one [`result`](</docs/en/agent-sdk/typescript#sdk-result-message>) message at the end.
+ * **Step:** a single request/response cycle within a `query()` call. Each step produces assistant messages with token usage.
+ * **Session:** a series of `query()` calls linked by a session ID (using the `resume` option). Each `query()` call within a session reports its own cost independently.
+
+The following diagram shows the message stream from a single `query()` call, with token usage reported at each step and the authoritative total at the end:
+
+1
+
+Each step produces assistant messages
+
+When Claude responds, it sends one or more assistant messages. In TypeScript, each assistant message contains a nested `BetaMessage` (accessed via `message.message`) with an `id` and a [`usage`](<https://platform.claude.com/docs/en/api/messages>) object with token counts (`input_tokens`, `output_tokens`). In Python, the `AssistantMessage` dataclass exposes the same data directly via `message.usage` and `message.message_id`. When Claude uses multiple tools in one turn, all messages in that turn share the same ID, so deduplicate by ID to avoid double-counting.
+
+2
+
+The result message provides the authoritative total
+
+When the `query()` call completes, the SDK emits a result message with `total_cost_usd` and cumulative `usage`. This is available in both TypeScript ([`SDKResultMessage`](</docs/en/agent-sdk/typescript#sdk-result-message>)) and Python ([`ResultMessage`](</docs/en/agent-sdk/python#result-message>)). If you make multiple `query()` calls (for example, in a multi-turn session), each result only reflects the cost of that individual call. If you only need the total cost, you can ignore the per-step usage and read this single value.
+
+##
+
+
+
+Get the total cost of a query
+
+The result message ([TypeScript](</docs/en/agent-sdk/typescript#sdk-result-message>), [Python](</docs/en/agent-sdk/python#result-message>)) is the last message in every `query()` call. It includes `total_cost_usd`, the cumulative cost across all steps in that call. This works for both success and error results. If you use sessions to make multiple `query()` calls, each result only reflects the cost of that individual call. The following examples iterate over the message stream from a `query()` call and print the total cost when the `result` message arrives:
+
+TypeScript
+
+Python
+
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ for await (const message of query({ prompt: "Summarize this project" })) {
+ if (message.type === "result") {
+ console.log(`Total cost: $${message.total_cost_usd}`);
+ }
+ }
+
+##
+
+
+
+Track per-step and per-model usage
+
+The examples in this section use TypeScript field names. In Python, the equivalent fields are [`AssistantMessage.usage`](</docs/en/agent-sdk/python#assistant-message>) and `AssistantMessage.message_id` for per-step usage, and [`ResultMessage.model_usage`](</docs/en/agent-sdk/python#result-message>) for per-model breakdowns.
+
+###
+
+
+
+Track per-step usage
+
+Each assistant message contains a nested `BetaMessage` (accessed via `message.message`) with an `id` and `usage` object with token counts. When Claude uses tools in parallel, multiple messages share the same `id` with identical usage data. Track which IDs you’ve already counted and skip duplicates to avoid inflated totals.
+
+Parallel tool calls produce multiple assistant messages whose nested `BetaMessage` shares the same `id` and identical usage. Always deduplicate by ID to get accurate per-step token counts.
+
+The following example accumulates input and output tokens across all steps, counting each unique message ID only once:
+
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ const seenIds = new Set<string>();
+ let totalInputTokens = 0;
+ let totalOutputTokens = 0;
+
+ for await (const message of query({ prompt: "Summarize this project" })) {
+ if (message.type === "assistant") {
+ const msgId = message.message.id;
+
+ // Parallel tool calls share the same ID, only count once
+ if (!seenIds.has(msgId)) {
+ seenIds.add(msgId);
+ totalInputTokens += message.message.usage.input_tokens;
+ totalOutputTokens += message.message.usage.output_tokens;
+ }
+ }
+ }
+
+ console.log(`Steps: ${seenIds.size}`);
+ console.log(`Input tokens: ${totalInputTokens}`);
+ console.log(`Output tokens: ${totalOutputTokens}`);
+
+###
+
+
+
+Break down usage per model
+
+The result message includes [`modelUsage`](</docs/en/agent-sdk/typescript#model-usage>), a map of model name to per-model token counts and cost. This is useful when you run multiple models (for example, Haiku for subagents and Opus for the main agent) and want to see where tokens are going. The following example runs a query and prints the cost and token breakdown for each model used:
+
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ for await (const message of query({ prompt: "Summarize this project" })) {
+ if (message.type !== "result") continue;
+
+ for (const [modelName, usage] of Object.entries(message.modelUsage)) {
+ console.log(`${modelName}: $${usage.costUSD.toFixed(4)}`);
+ console.log(` Input tokens: ${usage.inputTokens}`);
+ console.log(` Output tokens: ${usage.outputTokens}`);
+ console.log(` Cache read: ${usage.cacheReadInputTokens}`);
+ console.log(` Cache creation: ${usage.cacheCreationInputTokens}`);
+ }
+ }
+
+##
+
+
+
+Accumulate costs across multiple calls
+
+Each `query()` call returns its own `total_cost_usd`. The SDK does not provide a session-level total, so if your application makes multiple `query()` calls (for example, in a multi-turn session or across different users), accumulate the totals yourself. The following examples run two `query()` calls sequentially, add each call’s `total_cost_usd` to a running total, and print both the per-call and combined cost:
+
+TypeScript
+
+Python
+
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ // Track cumulative cost across multiple query() calls
+ let totalSpend = 0;
+
+ const prompts = [
+ "Read the files in src/ and summarize the architecture",
+ "List all exported functions in src/auth.ts"
+ ];
+
+ for (const prompt of prompts) {
+ for await (const message of query({ prompt })) {
+ if (message.type === "result") {
+ totalSpend += message.total_cost_usd;
+ console.log(`This call: $${message.total_cost_usd}`);
+ }
+ }
+ }
+
+ console.log(`Total spend: $${totalSpend.toFixed(4)}`);
+
+##
+
+
+
+Handle errors, caching, and token discrepancies
+
+For accurate cost tracking, account for failed conversations, cache token pricing, and occasional reporting inconsistencies.
+
+###
+
+
+
+Resolve output token discrepancies
+
+In rare cases, you might observe different `output_tokens` values for messages with the same ID. When this occurs:
+
+ 1. **Use the highest value:** the final message in a group typically contains the accurate total.
+ 2. **Verify against total cost:** the `total_cost_usd` in the result message is authoritative.
+ 3. **Report inconsistencies:** file issues at the [Claude Code GitHub repository](<https://github.com/anthropics/claude-code/issues>).
+
+###
+
+
+
+Track costs on failed conversations
+
+Both success and error result messages include `usage` and `total_cost_usd`. If a conversation fails mid-way, you still consumed tokens up to the point of failure. Always read cost data from the result message regardless of its `subtype`.
+
+###
+
+
+
+Track cache tokens
+
+The Agent SDK automatically uses [prompt caching](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching>) to reduce costs on repeated content. You do not need to configure caching yourself. The usage object includes two additional fields for cache tracking:
+
+ * `cache_creation_input_tokens`: tokens used to create new cache entries (charged at a higher rate than standard input tokens).
+ * `cache_read_input_tokens`: tokens read from existing cache entries (charged at a reduced rate).
+
+Track these separately from `input_tokens` to understand caching savings. In TypeScript, these fields are typed on the [`Usage`](</docs/en/agent-sdk/typescript#usage>) object. In Python, they appear as keys in the [`ResultMessage.usage`](</docs/en/agent-sdk/python#result-message>) dict (for example, `message.usage.get("cache_read_input_tokens", 0)`).
+
+##
+
+
+
+Related documentation
+
+ * [TypeScript SDK Reference](</docs/en/agent-sdk/typescript>) \- Complete API documentation
+ * [SDK Overview](</docs/en/agent-sdk/overview>) \- Getting started with the SDK
+ * [SDK Permissions](</docs/en/agent-sdk/permissions>) \- Managing tool permissions