在 OpenCode CLI 或类似 Agent 中通过 OpenRouter 接入 Qwen3.7-Max 时,必须通过 @ai-sdk/alibaba 适配层显式透传上下文缓存请求头,并避免在 AGENTS.md 中使用未绑定的间接工具规则,以防单次会话 8M+ Token 暴走。
适合的任务:通过 OpenCode CLI、Claude Code 或自建 Agent 架构运行 Qwen3.7-Max 代码工程与复杂重构。
不适合的任务:未配置缓存或在不可观测环境下直接让模型自主调用高频 CLI 工具。
适用的模型版本:qwen/qwen-3.7-max(OpenRouter 别名)或阿里云官方 qwen3.7-max。
适用的客户端、Agent 或 API:OpenCode CLI、Claude Code 兼容层、OpenRouter API、DashScope API。
推荐的推理档位和参数:系统提示词置顶固定前缀以实现 Prompt Caching,关闭开放式非严格匹配指令。
opencode.json 配置{
"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)# 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.在 opencode.json 中配置 provider 为带有 @ai-sdk/alibaba 适配的 OpenRouter 别名,确保请求透传缓存命中标头。
检查 AGENTS.md / CLAUDE.md 中的所有指令:确保每个规则引用的工具名称与模型实际可调用的工具清单逐字一致(Verbatim matching)。
启动小范围测试:执行 3 次代码检索与修改交互,通过代理日志监控每轮输入 Token 是否呈恒定增长(未缓存)或仅增加增量(已命中缓存)。
在多轮对话中监控首 Token 延迟(TTFT)与输出 Token 速率,若发现模型反复重新解释规范或循环调用工具,立即触发 Early Stop。
社区用户 Unable_Strategy 实测记录:在 OpenCode CLI 中直接使用标准 OpenRouter 配置调用 Qwen3.7-Max,8 分钟内因每次交互全量重新加载项目上下文且未命中缓存,迅速消耗 $4.00 API 额度(134 次请求产生 8,236,560 Input Tokens,40,903 Output Tokens)。
经调试发现,OpenCode CLI 的缓存头注入依赖 SDK 判断,指定 npm: "@ai-sdk/alibaba" 后成功激活服务端 Prompt Caching,大幅降低重复输入开销。
架构分析师 Deep_Ad1959 指出:Qwen3 系列在解析多层级自然语言 Agent 规则时,若指令中的工具描述与实际提供的 Tool Schema 不完全一致,模型会倾向于将指令降级为自由重写,进而引发长上下文循环。
本配置依赖 OpenRouter 与 Alibaba 兼容端点的 Prompt Caching 实现,第三方私有中转站若不支持缓存头透传则无法生效。
规则约束可以降低误操作概率,但对极端复杂的负向指令(如带有双重否定的配置变更),仍需结合沙箱与人机确认机制。
社区核心讨论指出:“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