OpenCode Zen free tier: old header spoof to zen/v1 is dead; /inference/v1 + prompt-prefix rule works (solved Oct 2026)
Symptom#
Calls from pi (or curl, or any non-opencode client) to the Zen free models return 403 FreeTierError, both on the documented https://opencode.ai/zen/v1 endpoint and — before the right body shape is used — on the new endpoint. Everything else in the request can be byte-identical to the real CLI and it still fails.
Cause#
Two-part gate as of 2026-10-15, found by MITM-capturing opencode CLI 1.19.0 and bisecting replays:
- zen/v1 is the LEGACY endpoint: it always 403s free SKUs from outside the CLI. The free lane lives on https://opencode.ai/inference/v1 (catalog at https://models.opencode.ai/api.json; CLI caches nothing needed).
- The gate pairs endpoint + template + tools: /chat/completions takes the CLI agent prompt when tools are sent and the CLI title prompt when not; /responses takes the title prompt only. Text may be appended after the verbatim prefix; edits inside the prefix 403. The prefix flips at 963/964 chars of the title template (startsWith against its head), so a 964-char head + a boundary note produces far better answer quality than the full title template. On the /responses path title+tools 403 (free Responses models are chat-only); the lane additionally caps a request at three input messages and max_output_tokens 65536 (131072 = the model's own catalog max, 403s). muse burns reasoning tokens first at the responses default effort high; set effort low.
- Authorization on /inference/v1 is flexible: Bearer public AND a REAL zen key (and even the opencode-go key) both return 200 on free SKUs once the shape is right. The key does not unlock anything extra; it just attributes the request.
Header-only spoofs still fail on both endpoints: TLS fingerprint, endpoints and headers are not enough — the body check is the moving part.
Fix#
pi extension config/pi/extensions/opencode-free.ts does all of it (initial agentic set committed b4a77d5):
- registerProvider complete Provider "opencode-free": the nine free openai-completions models plus muse-spark-1.3-contributor-free (openai-responses) from getBuiltinModels("opencode") with baseUrl rewritten to https://opencode.ai/inference/v1; auth.resolve returns "public" (attaching the stored Zen key is prepared, commented out — it verifies 200).
- stream/streamSimple delegate to pi-ai's openAICompletionsApi / openAIResponsesApi but pass a custom fetch (options.fetch): OpenAI SDK needs it — its own undici adds x-stainless-/accept-language/sec-fetch-mode and plain globalThis.fetch patching is skipped on that path. The custom fetch mints ses_+26-char ids (descending) and msg_+26 (ascending) per call via node:https (raw TLS, no undici browser headers), strips x-stainless-, forces accept /.
- withAccessPrompt prepends a system message: verbatim CLI agent prompt (tools present) or the full CLI title prompt (none), each + an override note. withResponsesGate (muse only) collapses the system prompt, keeps at most three messages, carries only the 964-char title prefix + the override note, and drops tool declarations. muse dispatches with reasoningEffort low and maxTokens clamped to 65536. Known wart: with pi's own 30 KB system prompt attached, muse's answers drift ("What are we working on?" instead of answering); impossible to fix provider-side because pi's prompt is pi's. The nine completions models answer properly and call tools. Also connects: pi-ai passes options.fetch into new OpenAI({fetch}) (dist/api/openai-completions.js), so no monkey-patching of globalThis is needed on the SDK path. Extension virtual modules only resolve: "@earendil-works/pi-ai", "@earendil-works/pi-ai/providers/all", "@earendil-works/pi-ai/compat", "@earendil-works/pi-ai/oauth", "@earendil-works/pi-coding-agent", "@earendil-works/pi-tui", "typebox*". You cannot import "@earendil-works/pi-ai/api/openai-completions" directly from an extension; the compat bundle re-exports openAICompletionsApi. Type check the extension with tsc strict against pi's node_modules (tsconfig lives in /tmp/pi-tc).
Evidence#
2026-10-15 MITM of the CLI (node CONNECT + self-signed, NODE_TLS_REJECT_UNAUTHORIZED=0): its request POST https://opencode.ai/inference/v1/chat/completions Authorization: Bearer public got upstream 200; identical headers to /zen/v1 are 403; body bisect: title-prefix+no-tools 200, agent-prefix+tools 200, agent-prefix+no-tools 403, title-prefix+tools 403; title head 964 chars + override on /responses 200 and answers "The capital of France is Paris." while the full title template answers "Capital of France inquiry" (drift); cap test: max_output_tokens 4096..65536 200, 131072 403 (muse); message count: 3 input messages 200, 4 403. Key test: title-gate shape with the stored zen key 200, and with the opencode-go key 200 (mimo-v2.6-flash-free). pi live: pi --model opencode-free/mimo-v2.6-flash-free -p "hi" streams with tool calls; muse -p runs 200 but off-task; opencode-go/glm-5.3-flash unaffected.