Repository navigation
Description
Recent changes to the OpenCode Zen gateway break free-tier model routing (deepseek-v4-flash-free, big-pickle, mimo-v2.5-free) when called from AlphaCode. Requests to https://opencode.ai/zen/v1 fail with 400 MissingSessionID or 403 FreeTierError: OpenCode's free tier can only be used from within OpenCode.
The gateway now verifies client metadata, payload structure, and transport properties. AlphaCode needs an adapter update to satisfy this verification contract.
Root Cause
OpenCode inspects the following layers on requests to /zen/v1:
-
Header Validation:
- Missing
x-opencode-sessiontriggers400 MissingSessionID. - User-Agent strings from standard HTTP libraries or OpenAI SDKs are blocked with
403 FreeTierError. - Standard paid API keys (
sk-*) are rejected on free models if no billing account is attached. The free tier requiresAuthorization: Bearer public. - Client telemetry headers (
x-opencode-client,x-opencode-project,x-opencode-request) are required.
- Missing
-
Payload Validation:
- Non-streaming calls (
stream: false) are rejected. - Requests lacking standard OpenCode tool definitions or valid tool schemas trigger rejection.
- Non-streaming calls (
Proposed Solution
Update the OpenCode provider adapter in AlphaCode to handle payload translation and header injection:
1. Header Injection
Inject the expected client headers on all requests routed to the Zen free tier:
Authorization: Bearer publicUser-Agent: opencode/1.18.31 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14x-opencode-client: clix-opencode-project: globalx-opencode-session: ses_<uuid>(persisted for the duration of the conversation thread)x-opencode-request: msg_<uuid>(regenerated per request turn)
2. Streaming Aggregation
Enforce "stream": true on all outbound requests to Zen. If AlphaCode caller code requests a non-streaming response, consume the SSE stream in the adapter and aggregate the chunks into a standard response object before returning.
3. Tool Payload Normalization
If the incoming prompt has no tool calls or empty tools, inject a placeholder tool definition with "tool_choice": "auto" to satisfy gateway schema checks.
Acceptance Criteria
- Outbound requests to
https://opencode.ai/zen/v1include the requiredx-opencode-*headers andBearer public. - Session IDs remain consistent per conversation; request IDs are unique per turn.
- Non-streaming requests are translated to SSE upstream and reassembled downstream.
- Free-tier models run end-to-end in AlphaCode without returning 400 or 403 errors.
Activity
fix(provider): harden cost tier validation and merge catalog tiers