{"slug": "claude-code-context-usage-diagnose-unnecessary-tokens", "title": "Claude Code Context Usage: Diagnose Unnecessary Tokens", "summary": "A developer detailed a method for diagnosing unnecessary token usage in Claude Code sessions, using native commands like /context and /cost to measure baseline context and compare A/B runs after a single reversible change. The approach was illustrated with a sample case where input tokens dropped from 18,420 to 6,180 after moving a large build log out of CLAUDE.md, without degrading task correctness. The developer emphasized controlled experiments and retaining changes only if all five criteria are met.", "body_md": "In the Claude Code community, a user reported that a fresh session started at roughly 35,000 tokens and later dropped to about 13,000 after cleaning up repository instructions. That is a useful reminder to inspect your own project setup, but not a universal benchmark: codebase size, instruction length, MCP tools, and measurement methods differ across projects.\n\nYou can inspect your setup in two controlled runs. Measure baseline context using native Claude Code commands and API metadata, modify exactly one reversible context source, and repeat the same request. Then compare token usage, cost, and task correctness.\n\nYou do not need to guess context sizes. Claude Code provides native commands:\n\n`/context`\n\n— displays the breakdown of active context: project instructions, connected MCP tools, cached files, and conversation history;`/cost`\n\n— outputs token consumption (input, output, cache) and estimated cost for the current session.For a separate API workflow, the API Dashboard provides per-request accounting: input tokens, output tokens, cache hits, HTTP status, and actual balance charges.\n\nStart with a small read-only query that does not modify files:\n\n```\nFind where validateInvoice is defined.\nName the function file and one test that checks an empty amount.\nDo not edit anything.\n```\n\nThis request produces a clear binary outcome: two correct file paths or a failure.\n\nBefore running, fix the baseline variables:\n\nOpen a fresh session, execute `/context`\n\nto inspect baseline load, run the test query, and record the metadata:\n\n| Field | Run A |\n|---|---|\n| Request status | 200 OK |\n| Input tokens | 18,420 |\n| Output tokens | 310 |\n| Cache tokens | 0 |\n| Cost / charge | $0.026 |\n| Result | correct function and test identified |\n\nThese figures illustrate the comparison worksheet and format verified on August 18, 2026 against [current BetterToken pricing](https://bettertoken.ai/pricing?utm_source=devto&utm_medium=syndication&utm_campaign=SEO-097&utm_content=claude-code-kontekst-i-usage-diagnostika-tokenov). In your own project, record the live numbers from `/cost`\n\nor your dashboard.\n\nDo not include API keys, private source code, full proprietary prompts, or sensitive logs in notes. Metadata, source names, and task outcomes are sufficient.\n\nDo not disable all configuration at once. Investigate context sources in priority order:\n\n`CLAUDE.md`\n\n)`/compact`\n\nor start fresh with a handoff if needed).Suppose `CLAUDE.md`\n\ncontains a 12,000-line build log. Make one reversible change: move the log to `docs/build.log`\n\nand keep a concise pointer in `CLAUDE.md`\n\n. Do not change the model, MCP tools, or prompt at the same time.\n\nKeep the revision, model, and prompt unchanged. After the single modification, rerun the request and complete the A/B comparison table:\n\n| Field | Run A | Run B | Difference |\n|---|---|---|---|\n| Request status | 200 OK | 200 OK | Success |\n| Input tokens | 18,420 | 6,180 | -12,240 |\n| Output tokens | 310 | 295 | -15 |\n| Cache tokens | 0 | 0 | 0 |\n| Cost / charge | $0.026 | $0.009 | -$0.017 |\n| Function found | yes | yes | unchanged |\n| Test found | yes | yes | unchanged |\n\nIf input tokens dropped but Run B identified the wrong test or ignored a mandatory project constraint, the change degraded performance. Restore the original instruction.\n\nRetain configuration changes only when all five criteria are met:\n\n`/cost`\n\nand the dashboard confirm expected metadata changes.For your own API workflow, the BetterToken Dashboard displays balance, active model, exact timestamp, HTTP status, input, output, and cache tokens, along with the resulting charge. Match records by timestamps from Runs A and B and record them in your comparison table. The dashboard stores request metadata without storing complete private prompt text or responses.\n\nBetterToken provides independent Anthropic-compatible API access with pay-as-you-go billing, separate from Claude.ai subscriptions. [Check the current BetterToken documentation for Claude Code](https://docs.bettertoken.ai/ai-tools/claude-code?utm_source=devto&utm_medium=syndication&utm_campaign=SEO-097&utm_content=claude-code-kontekst-i-usage-diagnostika-tokenov), generate an API Key in your workspace, and track token usage directly in the dashboard.\n\nThe purpose of context optimization is removing irrelevant noise without losing essential context. Measure baseline context with `/context`\n\nand `/cost`\n\n, adjust one source at a time, verify task accuracy, and cross-reference API usage metadata.\n\n*Originally published on the BetterToken blog.*\n\nBetterToken provides pay-as-you-go access to AI model APIs through\n\nOpenAI-compatible and Anthropic-compatible endpoints — useful if you are wiring\n\nClaude Code, Codex, or your own tooling to a custom base URL.\n\nSee the [docs](https://docs.bettertoken.ai/?utm_source=devto&utm_medium=syndication&utm_campaign=SEO-097&utm_content=claude-code-context-usage-token-diagnostics) to get started.", "url": "https://wpnews.pro/news/claude-code-context-usage-diagnose-unnecessary-tokens", "canonical_source": "https://dev.to/bettertoken_ai/claude-code-context-usage-diagnose-unnecessary-tokens-476e", "published_at": "2026-08-20 08:18:24+00:00", "updated_at": "2026-08-20 08:44:48.155142+00:00", "lang": "en", "topics": ["developer-tools", "large-language-models", "ai-tools"], "entities": ["Claude Code", "BetterToken", "Anthropic"], "alternates": {"html": "https://wpnews.pro/news/claude-code-context-usage-diagnose-unnecessary-tokens", "markdown": "https://wpnews.pro/news/claude-code-context-usage-diagnose-unnecessary-tokens.md", "text": "https://wpnews.pro/news/claude-code-context-usage-diagnose-unnecessary-tokens.txt", "jsonld": "https://wpnews.pro/news/claude-code-context-usage-diagnose-unnecessary-tokens.jsonld"}}