feat: add get_context_usage() method to ClaudeSDKClient#764
Conversation
Adds ClaudeSDKClient.get_context_usage() which returns a breakdown of the current context window usage by category, matching the data shown by the /context command in the CLI. The CLI already supports the get_context_usage control request and the TypeScript SDK exposes it as getContextUsage() on the Query interface. This brings the Python SDK to parity. Returns a ContextUsageResponse TypedDict with: - categories: per-category token breakdown (system prompt, tools, messages) - totalTokens / maxTokens / percentage: overall usage - mcpTools / memoryFiles / agents: detailed per-item breakdowns - messageBreakdown / skills / slashCommands: optional detailed sections Fixes #507
There was a problem hiding this comment.
LGTM, straightforward addition following the established get_mcp_status() pattern.
Extended reasoning...
Overview
This PR adds a get_context_usage() method to ClaudeSDKClient across 5 files: new TypedDicts in types.py, a 3-line delegate in query.py, a public wrapper in client.py, exports in __init__.py, and 2 tests in test_streaming_client.py. Every layer follows the exact same pattern as the existing get_mcp_status() method.
Security risks
None. The method sends a read-only control request (get_context_usage) to the CLI subprocess and returns the response. No user input is interpolated, no new permissions are granted, and no external systems are contacted.
Level of scrutiny
Low scrutiny appropriate. This is a mechanical, pattern-following addition with no novel logic. The TypedDict fields use dict[str, Any] for nested detail fields (forward-compatible), and the required fields match the CLI wire format. The tests mirror the existing test_get_mcp_status test structure exactly.
Other factors
- No CODEOWNERS file blocking approval.
- No outstanding reviewer comments.
- No prior reviews from me on this PR.
- The 2 new tests cover the happy path (shape verification) and the error path (not-connected). Test structure is copy-paste from the MCP status tests with appropriate substitutions.
Summary
Adds
ClaudeSDKClient.get_context_usage()which returns a breakdown of the current context window usage by category, matching the data shown by the/contextcommand in the CLI.The CLI already supports the
get_context_usagecontrol request and the TypeScript SDK exposes it asgetContextUsage()on theQueryinterface (shipped in v0.2.86). This brings the Python SDK to parity.Fixes #507
Usage
Changes
types.py: AddContextUsageCategoryandContextUsageResponseTypedDicts_internal/query.py: Addget_context_usage()that sends{"subtype": "get_context_usage"}control requestclient.py: Add publicget_context_usage()wrapper following the existingget_mcp_status()pattern__init__.py: Export the new typestest_streaming_client.py: Add 2 tests (shape verification + not-connected error)Response shape
ContextUsageResponseincludes:categories— per-category token breakdown (system prompt, tools, messages, etc.)totalTokens/maxTokens/rawMaxTokens/percentage— overall usagemodel— which model the usage is calculated formcpTools/memoryFiles/agents— detailed per-item breakdownsmessageBreakdown/skills/slashCommands/systemTools— optional detailed sectionsisAutoCompactEnabled/autoCompactThreshold— autocompact stateNested detail fields use
list[dict[str, Any]]rather than fully-typed nested TypedDicts to stay forward-compatible as the CLI adds fields.Verification
ruff check+mypy src/cleanget_context_usage()returned 6 categories with correct token counts, model name, and percentage after a simple query