tokenade
Log inenfr
Start free

How to Reduce Qwen Code Token Usage

Qwen Code ships unusually good cost tooling: /context detail, /stats, an eager-tool allowlist, a compaction model. Here's which settings move the bill and what they leave untouched.

TL;DR — Read /context detail on the first turn to price the fixed prefix, trim it with tools.eager, use API-key auth so token caching applies, and compact with /compress-fast or a cheaper compactionModel. Then stop whole files and raw shell output entering the chat: no Qwen Code setting does that, Tokenade's hooks do.

Profile photo of Paul Irolla

By Paul Irolla

Founder · AI & developer tools · Tokenade

Ph.D. in AI · builds token-optimization tooling for AI agents

View author page
7 min read
Cite this page

Part of the reduce AI agent token usage pillar. Using another open terminal agent too? The opencode version covers the same levers there. This page is Qwen Code-specific: which settings exist, what each one saves, and what none of them reach.

How do you reduce Qwen Code token usage?

Measure the fixed prefix with /context detail, trim it with tools.eager, make sure token caching is active, and compact early with /compress-fast or a cheaper compactionModel. Then fix the biggest line item, which no setting removes: whole files and raw command output entering the conversation.

Qwen Code has better native cost tooling than most terminal agents. Its docs have a whole page on what they call resident context cost, and /stats reports cache hits explicitly. That makes the diagnosis easy. It doesn't make the session cheap, because every setting below acts either on the prefix or after the fact.

Sources: every setting on this page was read in the Qwen Code docs on 14 October 2026. Tokenade's figure comes from the open token optimizer benchmark.

Why does Qwen Code burn tokens?

Because each request carries a fixed prefix plus the whole conversation so far, and both grow faster than the work you ask for.

The Qwen Code docs spell out the prefix: the system prompt, the schema of every declared tool, your QWEN.md context files and the skill listing. You pay for it on every turn, "including the turns that just answer a question", as the resident context cost page puts it.

On top of that sits the conversation. Turn 30 re-sends turns 1 to 29: every file read, every grep result, every test log. A 2,000-line log read at turn 5 is billed again on each turn that follows, unless caching discounts it or compaction drops it.

So there are two problems. The prefix is a fixed tax per request; the history is a compounding one. Qwen Code gives you good tools for the first and decent ones for the second.

How do you see where Qwen Code's tokens go?

Run /context detail on the first turn of a session, then /stats at the end.

  • /context prints the context window breakdown by category. /context detail adds a row per built-in tool, MCP tool, context file and skill. Read it before your first prompt, while everything you see is prefix.
  • /stats (alias /usage) opens a usage dashboard. /stats model gives a per-model token breakdown with estimated cost, /stats tools counts tool calls, and /stats daily or /stats monthly show the trend.
  • modelPricing in settings.json feeds your real per-million rates to /stats model, so its cost estimate matches your provider.

The docs make a point worth stealing: don't judge the prefix as a share of the window. The same configuration reads 6.5% on a 1M-context model and 37% on a 128k one. They use "idle cost" instead: the input tokens of a session that asks one question and calls no tool.

Which built-in settings actually cut the bill?

Five of them, in rough order of return.

  1. Token caching, through an API key. The token caching page says caching is automatic with API-key authentication (Qwen API key or OpenAI-compatible providers), not with OAuth. /stats then shows how many input tokens came from cache.
  2. Turn off features you don't use. The docs rank this first: each declared tool adds its schema to every request, and disabling a feature also removes its tools from subagents.
  3. tools.eager. An allowlist of built-in tools whose schemas stay in the first request. The rest become deferred and are reached through tool_search when needed. It isn't a disable: that takes tools.disabled or a permissions.deny rule.
  4. context.autoCompactThreshold. Auto-compaction fires at 85% of the context window by default. Lower it and less history rides along. It replaces model.chatCompression.contextPercentageThreshold, which is now silently ignored.
  5. compactionModel. The model that writes the compaction summary. Empty means the main model; the docs note a smaller one can reduce compression latency and cost. Set it with /model --compaction <model-id>.

Three commands complete the set, and they're not interchangeable:

  • /compress (alias /summarize) replaces the history with a model-written summary. It's destructive. /summary is the non-destructive one: it writes .qwen/PROJECT_SUMMARY.md and leaves the chat alone.
  • /compress-fast compresses without an AI call. It strips old tool outputs and thinking parts, which is exactly the bloat after a long debugging stretch.
  • /clear (also /reset, /new) drops the history entirely. When the task changes, it's the right one.

Should you cap Qwen Code's output tokens?

Only as a guard rail. Output is rarely where the bill lives.

Two knobs exist. QWEN_CODE_MAX_OUTPUT_TOKENS overrides the default maximum output per response, and samplingParams.max_tokens in settings.json takes precedence over it. Without either, Qwen Code uses the model's declared output limit and may retry with an escalated limit (64K floor) when a response is truncated.

Setting either one disables that escalation, per the settings reference. That's the trade: a fixed ceiling against possibly truncated answers on big edits. The docs pitch it mainly for self-hosted backends that want a smaller per-request reservation.

Two input-side guards matter more:

  • model.sessionTokenLimit drops the next send once the recorded prompt count exceeds the limit. The default, -1, means unlimited. It's a hard stop for a runaway session.
  • tools.truncateToolOutputThreshold and tools.truncateToolOutputLines truncate Shell, Grep, Glob, ReadFile and ReadManyFiles output above 25,000 characters or 1,000 lines by default.

Truncation is blunt, though. It cuts by size, not by relevance, so nothing guarantees the failing assertion in a long test log is the part that survives.

What do Qwen Code's settings leave untouched?

What the agent reads mid-session: whole files, raw shell output, MCP results.

Caching discounts repeated context, but the first time a 6,000-token file enters, you pay full price plus the cache write. Compaction acts after the fact: the file already rode along on every turn before the threshold. tools.eager shrinks schemas, not results.

That's the slice Tokenade works on. On Qwen Code, tokenade install adds hooks to ~/.qwen/settings.json (PreToolUse, SessionStart, UserPromptSubmit), so shell output reaches the model folded to its decisive lines. It also wraps your MCP servers and writes its rules to ~/.qwen/QWEN.md.

Two caveats, both in the compatibility matrix. Qwen Code only runs hooks with hooks.enabled: true in ~/.qwen/settings.json. And on Windows, cmd.exe can't parse the hook's quoting, so command output is folded by the local LLM proxy instead: that cell reads Partial+. The full technical brief is at tokenade.net/agents/qwen-code.md.

On the open token optimizer benchmark it cuts session cost by 39% on long sessions against running no tool at all. That campaign ran on Claude Code, not Qwen Code: the mechanism is the same, the measurement isn't.

How to apply this today

  1. Open a fresh session and run /context detail before typing anything. Note the three biggest rows.
  2. If you pay per token, use API-key auth so caching applies, and check the cached share in /stats.
  3. Disable features you never use, then set tools.eager to the built-in tools you actually call.
  4. Set compactionModel to a cheaper model, and consider lowering context.autoCompactThreshold.
  5. Run /compress-fast after a noisy debugging stretch and /clear when the task changes.
  6. Then attack reads and logs: install Tokenade, and confirm hooks.enabled is true.

What goes wrong (anti-patterns)

Reading the window percentage. A prefix at 6% of a 1M window costs exactly what it costs at 37% of 128k. Track idle cost instead.

Typing /summarize when you meant /summary. One rewrites your history, the other writes a file. The alias makes the mistake easy.

Capping output to save money. Output caps disable escalation and can truncate a large edit, which you then pay to redo. The bill is mostly input.

Raising truncation limits to "see everything". Every extra line of log is re-sent on each later turn. If you need the failure, you need a filter, not a bigger window.

FAQ

Does Qwen Code cache tokens automatically?

Yes, with API-key authentication: Qwen API keys and OpenAI-compatible providers. The docs say caching is automatic there and stores frequently used content such as system instructions and conversation history. It does not apply with OAuth. /stats shows how many input tokens were served from cache when caching is active.

How do I see Qwen Code token usage?

Use /stats (alias /usage) for the dashboard, /stats model for per-model tokens and estimated cost, and /stats daily or /stats monthly for trends. For the composition of one request, run /context detail on the first turn: it lists every tool schema, context file and skill in the prefix.

What is the difference between /compress and /compress-fast?

/compress asks a model to summarise the conversation and replaces the history with that summary. /compress-fast uses no AI call: it strips old tool outputs and thinking parts. The fast one is cheaper and suits sessions bloated by logs. /summary is different again: it writes a project summary file without touching the chat.

How do I limit Qwen Code output tokens?

Set samplingParams.max_tokens in settings.json, or the QWEN_CODE_MAX_OUTPUT_TOKENS environment variable; the setting wins over the variable. Either one disables Qwen Code's automatic output escalation. For a hard stop on input, use model.sessionTokenLimit, which drops the next send once the recorded prompt count exceeds it.

Does Tokenade work with Qwen Code on Windows?

Yes. On Linux and macOS, hooks fold command output directly. On Windows, cmd.exe can't parse the hook's quoting, so command output goes through Tokenade's local LLM proxy instead, which tokenade install sets up through env.OPENAI_BASE_URL in settings.json. Every other feature is covered on all three systems.


See also:

FAQ

Cut your AI agent's token bill.

Ranked #1 on the Token-Harness Optimizer Leaderboard. Zero config.

Tokenade cuts the token bill of AI agents: one install, zero config, and it trims what your agent sends to the model.