Auditing Agentic Context Windows with deepagents-code
The deepagents-code project introduces a diagnostic tool to quantify token usage and optimize the management of agentic context windows.
The deepagents-code project introduces a diagnostic tool to quantify token usage and optimize the management of agentic context windows.

The rapid adoption of autonomous agents has shifted the focus of model optimization from parameter counts to the management of the context window. Developers now face the challenge of quantifying the overhead introduced by system prompts, persistent memory blocks, and tool schemas as these components grow in complexity.
According to the deepagents-code repository documentation, the project has introduced a diagnostic utility named /context-doctor to address this lack of transparency. This read-only command audits an active session to report estimated token costs per component without triggering a model inference call. It provides a granular breakdown of system prompts, project memory, skills, and Model Context Protocol (MCP) server configurations.
The estimation logic relies on a standard heuristic, calculating tokens by dividing character counts by four. This approach allows the tool to generate a report for each component, which is then compared against the provider-reported usage metadata once a live request is completed. The implementation of this feature, detailed in pull request #5830 and subsequently ported to the Talon Discord bot in PR #6557, enables developers to identify which parts of the agent configuration contribute most significantly to token bloat.
Empirical testing within the dcode repository reveals a notable discrepancy between static estimates and actual provider billing. In a session with two configured MCP servers and loaded skills, the tool estimated an injected context of approximately 14,039 tokens. However, the actual usage metadata reported by Anthropic for the same session indicated a significantly higher billing figure, highlighting the impact of middleware and provider-side overhead.
The integration of Model Context Protocol servers further complicates these estimations, as each server introduces additional JSON-serialized schemas into the context window. When multiple MCP servers are active, the cumulative token cost of their tool definitions can quickly consume a significant portion of the available window. The /context-doctor tool allows developers to isolate these costs, providing a per-server breakdown that reveals the hidden price of modular agent extensions.
The discrepancy is further clarified by examining the usage metadata through the deepagents-thread-inspector skill. By reconstructing message history and analyzing the raw usage_metadata, researchers can observe how cache creation and cache read operations influence the final token count. This analysis confirms that while the initial request incurs a high cost for writing the static prefix to cache, subsequent turns benefit from cache hits, provided the context remains consistent.
The /context-doctor report explicitly flags this unattributed delta, offering a transparent view of the difference between estimated and billed tokens. This design choice prevents developers from assuming the estimate is an exact representation of provider billing. It instead serves as a diagnostic baseline to evaluate the efficiency of injected context components.
Effective context engineering requires a precise understanding of the relationship between local configuration and remote execution costs. The ability to audit these variables without incurring additional inference costs allows for iterative refinement of agent architecture. By isolating the impact of specific tools and memory blocks, developers can optimize their agent configurations to maintain performance while managing operational expenditure.
The integration of this tool into the development workflow signifies a shift toward more rigorous resource management in agentic systems. By providing a clear view of how system prompts and tool definitions consume the context window, developers can make informed decisions about which components to prune or optimize. This level of visibility is essential for maintaining the stability of complex agents that rely on extensive external data sources.
Future iterations of context auditing tools will likely need to account for more complex middleware additions and hidden provider overheads. The current implementation provides a necessary framework for quantifying these factors, moving beyond simple token counting toward a more comprehensive model of agentic resource management. Continued refinement of these diagnostic methods remains essential for scaling agentic systems in production environments.