When integrating Qwen3.7-Max via OpenRouter in OpenCode CLI or similar agent frameworks, you must explicitly pass context-caching request headers through the @ai-sdk/alibaba adapter layer and avoid unbound indirect tool rules in AGENTS.md to prevent single-session token runaways exceeding 8M+ tokens.
Suitable tasks: Running Qwen3.7-Max for code engineering and complex refactoring via OpenCode CLI, Claude Code, or custom agent architectures.
Unsuitable tasks: Allowing the model to autonomously invoke high-frequency CLI tools without cache configuration or in an unobservable environment.
Applicable model versions: qwen/qwen-3.7-max (OpenRouter alias ) or Alibaba Cloud official qwen3.7-max.
Applicable clients, agents, or APIs: OpenCode CLI, Claude Code compatibility layer, OpenRouter API, and DashScope API.
Recommended reasoning levels and parameters: Place a fixed prefix at the top of the system prompt to enable Prompt Caching, and disable open-ended, non-strict matching instructions.
opencode.json configuration to enable OpenRouter context caching in OpenCode CLI{
"provider": {
"openrouter-cached": {
"npm": "@ai-sdk/alibaba",
"name": "OpenRouter (Cached)",
"options": {
"baseURL": "https://openrouter.ai/api/v1",
"apiKey": "YOUR_OPENROUTER_API_KEY"
},
"models": {
"qwen/qwen-3.7-max": {
"name": "Qwen 3.7 Max"
}
}
}
}
}AGENTS.md ) to prevent repetitive context rewrites and token runaways with Qwen3.7-Max# Agent Execution Guardrails for Qwen3.7-Max
## Core Execution Rules
1. Tool Invocation Mapping:
- When reading repository context, use ONLY the exact registered tool name `grep_search` or `view_file`.
- Do NOT delegate tool actions to external secondary LLMs unless explicitly invoked via verified MCP endpoint.
2. Context Conservation:
- Do NOT re-read or dump unmodified configuration files on every iteration.
- For file modifications, output precise diff chunks instead of whole-file rewrites.
3. Negative Constraints & Deletion Protection:
- "Disable feature" MUST be interpreted as disabling logic (e.g., commenting out, flipping boolean flags, or setting feature toggle to false).
- NEVER execute `rm`, `unlink`, or delete files/routes unless the prompt explicitly contains the word "DELETE" or "REMOVE".
- If unsure between disabling vs deleting, STOP and ask the user for confirmation.Configure the provider in opencode.json as an OpenRouter alias with the @ai-sdk/alibaba adapter to ensure requests pass through cache-hit headers.
Audit all directives in AGENTS.md / CLAUDE.md: ensure every tool name referenced by a rule matches the model's actual callable tool list verbatim (Verbatim matching ).
Run a small-scale test: execute 3 code retrieval and modification interactions, and monitor via proxy logs whether input tokens grow linearly on each turn (uncached ) or only increase by deltas (cache hit ).
Monitor Time to First Token (TTFT ) and output token generation speed across multi-turn conversations; immediately trigger an Early Stop if the model repeatedly re-interprets specifications or enters tool-invocation loops.
Empirical test log by community user Unable_Strategy: using the standard OpenRouter configuration to call Qwen3.7-Max directly in OpenCode CLI consumed $4.00 in API credits within 8 minutes (134 requests generating 8,236,560 input tokens and 40,903 output tokens ) because the entire project context was reloaded on every interaction without hitting the cache.
Debugging revealed that OpenCode CLI's cache-header injection relies on SDK detection; specifying npm: "@ai-sdk/alibaba" successfully activates server-side Prompt Caching, dramatically cutting repetitive input token costs.
Architectural analyst Deep_Ad1959 pointed out: when the Qwen3 series parses multi-layered natural language agent rules, if tool descriptions in instructions do not match the actually provided Tool Schema verbatim, the model tends to degrade instructions into open-ended rewrites, triggering long-context loops.
This configuration relies on the Prompt Caching implementation of OpenRouter and Alibaba-compatible endpoints; third-party private proxies will not work if they do not support cache-header passthrough.
Rule-based constraints reduce the probability of unintended operations, but extremely complex negative instructions (such as configuration changes containing double negatives ) still require sandboxing and human-in-the-loop confirmation mechanisms.
A core community discussion noted: "8.2M input on 3 prompts is the agent.md getting re-read with most of the repo on every turn... grep your agent.md for every directive that depends on a tool name, and check if that tool name appears verbatim".
Qwen3.7 Max