opencode-sub2api-haiku-fix
v1.0.3
Published
OpenCode plugin for Haiku-specific Anthropic request compatibility through Sub2API.
Maintainers
Readme
opencode-sub2api-haiku-fix
A focused OpenCode plugin that fixes Claude Haiku request-shape failures through Sub2API without changing Sub2API, OpenCode core, or OMO.
It is intentionally narrow: it only applies to the built-in anthropic provider when the model id contains haiku.
Why
In this setup, Opus and Sonnet can work through the same Sub2API account while Haiku fails with:
You're out of extra usage. Add more at claude.ai/settings/usage and keep going.That message is misleading here. The account and Sub2API route are usable; Haiku is being rejected because the final request fingerprint does not look enough like Claude Code.
This plugin restores the missing Claude Code-style request shape on the OpenCode side.
What It Changes
- Preserves Haiku thinking with
thinking: { type: "enabled", budget_tokens }. - Rewrites the final Haiku wire body into a Claude Code-style 3-block system shape:
- billing attribution
- Claude Code identity
- cached general CLI prompt block
- Moves the original OpenCode system prompt into the opening messages so local instructions are not lost.
- Adds Claude Code-style
metadata.user_id,x-client-request-id, andx-claude-code-session-id. - Rewrites Haiku headers to the current Claude Code-compatible Anthropic shape.
- Adds
?beta=trueto Haiku/v1/messagesURLs. - Removes fields that break this Haiku path, including
context_managementandoutput_config.effort. - Caps Haiku tool definitions to
OPENCODE_HAIKU_MAX_TOOLS, defaulting to32.
It does not rewrite Sonnet or Opus requests. It does not implement auth, OAuth, token refresh, keychain access, account switching, or a proxy.
Install
Add the npm package to ~/.config/opencode/opencode.jsonc:
{
"plugin": [
"opencode-sub2api-haiku-fix@latest"
]
}Restart OpenCode after changing the plugin list.
Verify
npm testThe test suite covers:
- current OpenCode
chat.paramsshape - final Anthropic fetch-body rewrite
- Haiku thinking preservation
- non-Haiku requests staying untouched
Links
- npm: https://www.npmjs.com/package/opencode-sub2api-haiku-fix
- Project site: https://mjshao.fun/opencode-sub2api-haiku-fix/
- Portfolio entry: https://mjshao.fun/work/opencode-sub2api-haiku-fix
- Release: https://github.com/nxxxsooo/opencode-sub2api-haiku-fix/releases/tag/v1.0.3
Credits
Inspired by:
Vacbo/opencode-anthropic-fixmarco-jardim/opencode-anthropic-fixdotCipher/opencode-claude-bridgeianjwhite99/opencode-with-claudeWei-Shaw/sub2api, whose Claude Code mimicry work made the failure mode diagnosable
这是什么
opencode-sub2api-haiku-fix 是一个聚焦的 OpenCode 插件,用来修复 OpenCode 通过 Sub2API 调用 Claude Haiku 时的请求形状问题。
它不改 Sub2API,不改 OpenCode core,不改 OMO,也不处理任何账号或鉴权。它只在 OpenCode 侧,把已经要发给 anthropic provider 的 Haiku 请求改成更接近 Claude Code CLI 的 wire shape。
为什么需要
同一个 Sub2API 账号、同一个后端下,Opus 和 Sonnet 可以正常工作,但 Haiku 可能报:
You're out of extra usage. Add more at claude.ai/settings/usage and keep going.在这个场景里,这句话不是额度结论,而是请求指纹被上游判进了错误路径。Haiku 的 system body、metadata、thinking shape 和 beta/header 组合需要更像 Claude Code。
它具体做什么
- 保住 Haiku 的 manual thinking。
- 把 Haiku 最终请求改成 Claude Code 风格的三段 system:
- billing attribution
- Claude Code identity
- 带缓存断点的通用 CLI prompt block
- 把原始 OpenCode system prompt 移进开头 messages,避免本地指令丢失。
- 注入
metadata.user_id、x-client-request-id、x-claude-code-session-id。 - 对齐 Claude Code 风格 headers 和 beta flags。
- 给
/v1/messages增加?beta=true。 - 去掉这条 Haiku 路径上会出问题的
context_management和output_config.effort。 - 将 Haiku tools 数量限制到
OPENCODE_HAIKU_MAX_TOOLS,默认32。
它不会改 Sonnet 或 Opus 请求。
安装
在 ~/.config/opencode/opencode.jsonc 里添加:
{
"plugin": [
"opencode-sub2api-haiku-fix@latest"
]
}改完后重启 OpenCode。
验证
npm test测试覆盖 current OpenCode hook 形状、最终 fetch body 改写、Haiku thinking 保留,以及非 Haiku 请求不被改动。
License
MIT
