Skip to content

Fix: Parse and price OpenCode Zen's endpoints - #1247

Merged
huang195 merged 3 commits into
rossoctl:mainfrom
huang195:fix/zen-parse-and-price
Oct 2, 2026
Merged

huang195 merged 3 commits into
rossoctl:mainfrom
huang195:fix/zen-parse-and-price

Conversation

@huang195

@huang195 huang195 commented Oct 2, 2026

Copy link
Copy Markdown
Member

OpenCode Zen serves each model on the endpoint of the SDK it speaks, and Cortex parsed only one of them. Calls on the others recorded no model, no tokens and no cost. Zen's free models were also reported as unpriced, which asks the operator for a rate that does not exist.

Parsing

  • /zen/v1/messages and /zen/go/v1/messages (Claude and most Qwen models) now go to the Anthropic Messages parser. That covers the request, the buffered response, the streamed frames, and a whole SSE body delivered as one terminal frame.
  • /zen/go/v1/chat/completions (the Go plan's OpenAI-dialect endpoint) joins /zen/v1/chat/completions on the OpenAI parser.
  • /zen/v1/responses (GPT, Grok) and Gemini's /zen/v1/models/<id> remain unparsed. They need parsers Cortex does not have. docs/agents/opencode.md says so.

Pricing

  • Zen's free models are shipped at a rate of zero, scoped to opencode.ai. These are every model whose id ends in -free, plus big-pickle. A call to one is a priced zero and counts toward coverage.
  • The rows are hand-kept in core/cost/pricing/bundled_free.go, beside the generated LiteLLM table rather than in it, like the shipped gateway discount. They ship because config cannot express zero: a tier with a rate of 0 is left unset, and an entry whose every rate is 0 fails at startup. bundled: false drops them.
  • A -free model on any other endpoint stays unpriced.
  • agentop pricing shows these rows as free rather than -, which reads as a missing rate. /pricing responses gain an omitempty free field for this.
  • A host view no longer lists shipped rows scoped to another endpoint, so --host api.anthropic.com stops showing Zen's free models as UNPRICED. Configured rows for other hosts are still listed.
  • docs/pricing.md covers what operators should know:
    • a configured catch-all for opencode.ai replaces the zero, so name Zen's paid models instead;
    • a zero still needs token counts to apply to;
    • a free call's cost cells in the events and sessions panes are blank.

Testing

  • New tests for each Zen path on the request side, both buffered OnResponse arms, streamed frames, and the whole-stream terminal frame. The request bodies carry fields only their own dialect reads, so a path sent to the wrong parser fails.
  • Pricing tests cover:
    • a priced zero on every tier, plus scoping to Zen's free models;
    • bundled: false, the free flag, host-view scoping, and a configured catch-all outranking the zero.
  • An agentop test drives the real handler and renderer.
  • Each new test was checked to fail with its fix reverted.
  • core passes go test -race ./.... agentop's tests pass, and cmd/cortex builds, vets and tests with the full and local profiles. gofmt and go mod tidy -diff are clean.

Assisted-By: Claude (Anthropic AI) [email protected]

Zen serves its Claude models, and most of its Qwen ones, on
/zen/v1/messages, and OpenCode's Go plan mounts both of Zen's
endpoints under /zen/go. Those paths fell to the parser's default
arm, so a request recorded no model, no tokens and no cost. One
predicate now names every Anthropic-dialect path, for the request and
every response-side call site alike.

Part of rossoctl#941.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
A call to one of Zen's free models was reported unpriced: Cortex ships
only Anthropic's rates, and config cannot say zero, because a rate of
0 leaves its tier unset. A hand-kept pair of rows beside the generated
table now prices Zen's "-free" models and big-pickle at zero, on
opencode.ai only, so the call is a priced zero. bundled: false drops
them with the rest of the shipped table. The OpenCode page and the
pricing doc say which Zen endpoints are parsed and how each is priced.

Part of rossoctl#941.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
agentop pricing rendered the zero-rate rows as "-", which reads as a
coverage gap, because a zero rate is omitted on the wire like an unset
tier. Describe and EffectiveFor now mark a row that prices every tier it
sets at zero as free, and agentop renders it so. EffectiveFor also stops
listing a shipped row scoped to another endpoint, so api.anthropic.com's
view no longer shows *-free and big-pickle as UNPRICED. Configured rows
for other hosts are still listed, as before.

The Zen request test now carries fields only its own dialect reads, so a
path routed to the wrong parser fails it. The buffered OnResponse path
and the whole-stream terminal frame get Zen tests too.

Docs: a configured catch-all for opencode.ai outranks the shipped zero,
a zero still needs token counts, and a free call's cost cells are blank.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
@huang195
huang195 requested a review from a team as a code owner October 2, 2026 22:22
@coderabbitai

coderabbitai Bot commented Oct 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 25 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8fad997a-399e-47c3-a9a2-a989c82bfd0a
📥 Commits

Reviewing files that changed from the base of the PR and between d55ddd7 and dcf2cfe.

📒 Files selected for processing (12)
  • cmd/agentop/cmd_pricing.go
  • cmd/agentop/cmd_pricing_test.go
  • core/cost/pricing/bundled_free.go
  • core/cost/pricing/bundled_free_test.go
  • core/cost/pricing/config.go
  • core/cost/pricing/describe.go
  • core/cost/pricing/rates.go
  • core/plugins/inferenceparser/anthropic.go
  • core/plugins/inferenceparser/plugin.go
  • core/plugins/inferenceparser/zen_test.go
  • docs/agents/opencode.md
  • docs/pricing.md
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mrsabath mrsabath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Checked out the PR head and exercised every behavioral claim against the real API rather than reading the description.

Pricing — all six claims hold exactly

Claim Measured
Zen -free + big-pickle are a priced zero prov=bundled, free=true, all four tiers set to 0
A -free model on another host stays unpriced api.anthropic.com / api.openai.com / example.com → prov=none
Non-free Zen models unaffected claude-sonnet-4-6 on Zen prices normally; qwen3-coder, free, freely → unpriced
Configured catch-all outranks the zero * for opencode.ai → prov=configured, input 3e-06
bundled: false drops the rows → prov=none
Host view no longer shows Zen rows elsewhere api.anthropic.com lists 0 Zen rows; opencode.ai lists both

Parsing — routing is exact-match, confirmed per path

Each test body carries only its own dialect's fields (Anthropic input_schema vs OpenAI function.parameters), so a tools=1 result proves the correct parser ran:

  • /zen/v1/messages, /zen/go/v1/messages, /v1/messages → Anthropic
  • /zen/v1/chat/completions, /zen/go/v1/chat/completions → OpenAI
  • /zen/v1/responses, /zen/v1/models/<id> → unparsed, as docs/agents/opencode.md states
  • Near-misses (/zen/go/go/v1/messages, /zen/v1/messages/extra) → unparsed, no leakage into the wrong parser

"Each test fails with its fix reverted"

Reverted both fixes independently and confirmed:

  • Dropping the Zen paths from isAnthropicMessagesPath fails all four response arms (buffered JSON, SSE, streamed frames, whole-stream terminal frame), each showing the exact tokens = 0/0 symptom the PR describes.
  • Commenting out bundledFreeRates() fails all three pricing tests.

go test -race ./cost/... ./plugins/inferenceparser/... clean, agentop pricing tests pass, gofmt -l . clean.

Two things worth noting

The host-scoping filter is the change with the widest reach, and it is correctly contained. if r.prov == ProvBundled && !matchHost(r.host, host) applies to every bundled row, not only Zen's, so I measured the blast radius rather than assuming it. The LiteLLM table ships all 33 rows under host "*", which matchHost treats as any-endpoint, so only Zen's 2 scoped rows are ever filtered. Every host view still shows 33 models, and api.anthropic.com went from 2 unpriced rows to 0 — no view loses anything it previously had.

Host matching has no suffix bleed. Since the zero rate is scoped by host, that scoping is worth probing: opencode.ai:443 and OPENCODE.AI match (port-stripped, case-insensitive), while api.opencode.ai, evil-opencode.ai and opencode.ai.attacker.com do not. A hostile lookalike cannot claim free pricing.

The Rates{Set: [...]bool{...}} construction is the right mechanism — it is the only way to express a genuine zero given that config leaves a 0 tier unset and rejects an all-zero entry, and the comment says exactly that. Keeping these rows in bundled_free.go rather than bundled.go, so make pricing-table cannot clobber them, is the same reasoning as bundledMultipliers and consistent with it.

Verdict

No must-fix, no suggestions, no nits. I went looking for scope leakage in the host filter and for suffix-matching weaknesses in the free rule, and found neither.

Replacing four scattered == anthropicMessagesPath comparisons with one predicate is the part that will keep paying off: it makes it structurally impossible for a path to be read as Anthropic on the way out and OpenAI on the way back, which is the bug class this PR is fixing.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants