Answer: Read the result message (SDKResultMessage in TypeScript, ResultMessage in Python) that 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 optional, so check that it isn't None before reading it. Status: Confirmed Entry: tool/claude-code/pricing-and-limits/total-cost-of-query Revision: 2 Digest: sha256:488ce19ac4564565 (changes whenever the content changes) Timeline: Created: 2026-11-13T03:02:39Z Last changed: 2026-11-17T12:20:20Z (result message type (Python): — → ResultMessage) Last verified: 2026-11-20T12:16:24Z Next check: 2026-11-21T12:16:24Z Check interval: Daily Expected change: No fixed schedule Data: Field | Value | Source | As of field name | total_cost_usd | [1] | 2026-11-20 result message type (TypeScript) | SDKResultMessage | [1] | 2026-11-20 result message type (Python) | ResultMessage | [1] | 2026-11-20 Python caveat | None | [1] | 2026-11-20 subagent inclusion | Included. Counts subagent requests alongside the top-level loop | [1] | 2026-11-20 Sources: [1] Anthropic. "Track cost and usage". https://code.claude.com/docs/en/agent-sdk/cost-tracking.md Role: primary. Accessed: 2026-11-20T12:16:24Z Quote: "It includes `total_cost_usd`, the cumulative estimated cost across all steps in that call." (Supports: field name) Quote: "typed as [`SDKResultMessage`](https://code.claude.com/docs/en/agent-sdk/typescript#sdkresultmessage) in TypeScript" (Supports: result message type (TypeScript)) Quote: "[`ResultMessage`](https://code.claude.com/docs/en/agent-sdk/python#resultmessage) in Python" (Supports: result message type (Python)) Quote: "In Python the field is typed as optional, so check that it isn't `None` before you read it." (Supports: Python caveat) Quote: "`total_cost_usd` | Included. Counts subagent requests alongside the top-level loop" (Supports: subagent inclusion) Published: 2026-11-20T02:52:51Z Snapshot SHA-256: 33162fd50e80cef6285d81742eaf4629de96a5a4354ab5f2e147dfb8a83af0d7 (captured 2026-11-17T12:20:22Z) Verification: Method: Automated extraction from [1] with deepseek-flash (JSON output, thinking disabled). Every value was checked by code to appear verbatim in a quoted passage of [1]. On this check the text of [1] was unchanged since 2026-11-17T12:20:20Z, so the values verified then were kept. Cross-check: not-available at 2026-11-20T12:16:24Z. No independent second source is configured for this entry Last successful check: 2026-11-20T12:16:24Z Last attempt: 2026-11-20T12:16:24Z Notes and limitations: Extracted automatically from a single official source (section "Get the total cost of a query"). Related: https://latestn.com/tool/claude-code/pricing-and-limits/ultrareview-diff-limits What are the diff size limits for Claude Code ultrareview and what happens if my diff is too large? https://latestn.com/tool/claude-code/pricing-and-limits/ultrareview-pricing-free-runs How much does Claude Code ultrareview cost and are there free runs? https://latestn.com/tool/claude-code/pricing-and-limits/websearch-session-limit What is the session search limit for the WebSearch tool in Claude Code? https://latestn.com/tool/claude-code/pricing-and-limits/scheduled-task-limits What are the usage limits for Claude Code scheduled tasks? https://latestn.com/tool/claude-code/pricing-and-limits/spend-limit-enforcement How does the Claude apps gateway enforce spend limits and what happens when a developer hits their cap? https://latestn.com/tool/claude-code/pricing-and-limits/tool-search-limits What are the limits of tool search in Claude Code? https://latestn.com/tool/ All entries in AI tools Change log: Time | Event | Change | Source 2026-11-17T12:20:20Z | changed | result message type (Python): — → ResultMessage | [1] 2026-11-17T12:20:20Z | changed | result message type (TypeScript): — → SDKResultMessage | [1] 2026-11-17T12:20:20Z | changed | subagent inclusion: Included → Included. Counts subagent requests alongside the top-level loop | [1] 2026-11-17T12:20:20Z | changed | Python result type: ResultMessage → — | [1] 2026-11-17T12:20:20Z | changed | TypeScript result type: SDKResultMessage → — | [1] 2026-11-13T03:02:39Z | added | Entry created | — Changes for this entry as a feed: https://latestn.com/tool/claude-code/pricing-and-limits/total-cost-of-query/latest Challenge this entry: Open challenges: None How: If a value here is wrong or out of date, send what is wrong and a link to evidence. Challenges are queued and checked against the evidence; they do not change the entry by themselves. If the entry changes as a result, the change log says so. Form: https://latestn.com/dispute/tool/claude-code/pricing-and-limits/total-cost-of-query POST: https://latestn.com/dispute/tool/claude-code/pricing-and-limits/total-cost-of-query with claim, evidence_url, and optionally field and contact (form-encoded or JSON) GET: https://latestn.com/dispute/tool/claude-code/pricing-and-limits/total-cost-of-query?claim={what is wrong}&evidence_url={link} How to cite: LatestN. (2026). How do I get the total cost of a query in the Claude Agent SDK? Revision 2, last verified 2026-11-20T12:16:24Z. https://latestn.com/tool/claude-code/pricing-and-limits/total-cost-of-query (accessed 2026-11-21T10:08:03Z). @misc{LatestN_tool_claude_code_pricing_and_limits_total_cost_of_query_r2, title = {How do I get the total cost of a query in the Claude Agent SDK?}, author = {{LatestN}}, year = {2026}, howpublished = {\url{https://latestn.com/tool/claude-code/pricing-and-limits/total-cost-of-query}}, note = {Revision 2; digest sha256:488ce19ac4564565; last verified 2026-11-20T12:16:24Z; accessed 2026-11-21T10:08:03Z} } Reuse: License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/) Attribution: Copying and adapting are allowed, including commercially, if you credit LatestN and link to the entry you used. Each entry ends with a ready-made citation. URL: https://latestn.com/tool/claude-code/pricing-and-limits/total-cost-of-query