- Python 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Work out per model whether `reasoning_effort` is accepted, which levels it takes, and send the level the user picked — only to models that have a dial. Capability detection reads every /models shape seen in the wild (supportedReasoningEfforts[].reasoningEffort, supported_parameters, supported_params, bare booleans like reasoning/capabilities.thinking, and a reasoning_effort list or default string). Only when upstream says nothing is a family heuristic used. Precedence, most specific first: NON_REASONING_MODELS, REASONING_EFFORT_OVERRIDES, upstream, REASONING_MODELS, heuristic. The pick is read from metadata.params.model_controls, body params model_controls and top-level model_controls (Open WebUI stashes controls in the metadata it hands a pipe), then params.reasoning_effort / top-level reasoning_effort / custom_params, then a per-chat /reasoning preference. Safety: reasoning_effort is no longer blindly passed through. A model without a dial never receives it, so a value left in Settings -> General cannot break an unrelated model. An unsupported pick (max on a model capped at high) maps to the nearest listed level with a status line. Nothing is sent unless someone asks, and background tasks (titles, tags, follow-ups) keep the provider default even when REASONING_EFFORT sets a floor. pipes() attaches a real info.params.model_controls.reasoning_effort block in the shape /api/v1/models stores, which lights up the knob on releases that read controls from a pipe. Current Open WebUI cannot render them for pipe models: routers/models.py rejects saving controls on a pipe-backed model and ModelControls.svelte filters out the pipe flag. So /reasoning (aliases /effort, /thinking) is the in-chat equivalent: bare lists levels and current state, `/reasoning high` persists per chat, `/reasoning default` clears it. Also: slash commands (including the pre-existing /usage) are skipped for background tasks, and REASONING_EFFORT defaults to empty rather than medium so existing requests are unchanged until someone picks a level. Tests: 40 -> 75 checks, all passing. Adds a .gitignore for __pycache__. |
||
| tests | ||
| .gitignore | ||
| opencode_go.py | ||
| README.md | ||
OpenCode Go for Open WebUI
An Open WebUI Pipe function that makes OpenCode Go / Go Plus / Go Teams behave the way OpenCode expects a client to behave, and that makes your Go subscription visible inside the UI:
- sends a stable
x-opencode-sessionheader per conversation (stable routing + warm prompt cache), - identifies itself properly (
x-opencode-client, ownUser-Agent, bearer key — no generic SDK UA), - surfaces per-request cost and Go allowance from
usage.opencode(token counter + toast), - renders a usage-credits dashboard (5-hour / weekly / monthly) from
GET /inference/go/v1/usage, plus locally tracked spend for the current chat and in total, - sends
reasoning_effortfor every model that actually takes it, honouring Open WebUI's Model Controls / Advanced Params / chat Controls picks — and never sending it to a model that has no thinking dial.
opencode_go.py ← the function (paste into Open WebUI)
tests/test_pipe.py ← offline harness with a fake Go endpoint (75 checks)
Why a Pipe and not a Filter?
A Filter cannot add HTTP headers to an OpenAI-connection request in Open WebUI. In
backend/open_webui/routers/openai.py the outgoing request is assembled as
payload = {**form_data} → JSONCodec.dumps(payload) → session.post(url, data=payload, headers=headers),
where headers comes only from get_headers_and_cookies() (connection config). So a filter that sets
body["extra_headers"] = {...} just puts a stray JSON field in the request body — the header never happens.
A Pipe owns the HTTP call, so it can send real per-conversation headers. That is the whole point here.
Install
- Admin Panel → Functions → ➕ (New Function), or Workspace → Functions depending on your version.
- Paste the entire content of
opencode_go.py, name itOpenCode Go, save. - Open the function's Valves and set:
API_KEY— anoc_sk_…key from OpenCode Console (Account → Personal API keys, or Settings → Service accounts for a shared key).MONTHLY_LIMIT_USD— optional, e.g.60for Go /180for Go Plus /65for Go Teams, so the dashboard can also show approximate dollars.0= percentages only.
- Enable the function (toggle). The models show up in the model picker as
OpenCode Go · <model>. - Optional, per user: Settings → Account → Functions (User valves) → give people their own key, plan label, or toast preference. A user key always wins over the shared one.
Reasoning effort
Models that expose a thinking dial get one here too. The pipe
- works out which models support
reasoning_effortand which levels they accept — from thesupportedReasoningEfforts/supported_parameters/reasoningfields of the upstream/modelsentry when it says anything, and from a family heuristic (o1…o9,gpt-5+,grok-*,gemini-3+,claude-*,qwen2/3*,deepseek*,glm-5+,kimi-k3+,minimax-m3+, …) when it does not; - reads the level the user picked, in this order: Model Controls →
params.reasoning_effort(Advanced Params, chat Controls, Settings → General, API) →custom_params.reasoning_effort→/reasoning <level>saved for the chat; - sends it as
reasoning_effort, mapping an unsupported pick (maxon a model that stops athigh) to the nearest accepted level and saying so in the status line; - strips the parameter for models with no dial, so a
reasoning_effortleft in your Settings → General never breaks a request to e.g.muse-spark-1.3-contributor.
A level is sent only when someone asks for one: with no pick anywhere, nothing is added and each
provider's own default applies. Background work (titles, tags, follow-ups) always keeps the provider
default, even when REASONING_EFFORT sets a floor.
Model Controls vs /reasoning
The pipe attaches a proper info.params.model_controls.reasoning_effort definition to each model it
lists, so the knob shows up on Open WebUI releases that accept controls from a pipe. On 0.12.3 / 0.13.x
it does not, for two upstream reasons: Workspace → Models refuses to save controls on a pipe-backed
model (“Model controls require a server-managed provider model.”), and the picker in the message input
skips any model carrying the pipe flag. EXPOSE_REASONING_CONTROL=false turns the extra metadata off.
So the in-chat equivalent is a command:
/reasoning → the levels this model accepts, and what is being sent right now
/reasoning high → remember `high` for this chat (persisted, survives restarts)
/reasoning default → forget the chat preference, go back to the provider default
/effort and /thinking are accepted as aliases. A saved chat preference is the lowest priority:
anything set in Model Controls, the chat's Controls, Settings → General, the model's Advanced Params or by
an API caller wins over it.
| Situation | What is sent |
|---|---|
Model has a dial, nobody picked a level, REASONING_EFFORT empty |
nothing (provider default) |
Model has a dial, nobody picked a level, REASONING_EFFORT=medium |
medium |
| Any source above picked a level the model lists | that level |
| A level the model does not list was picked | the nearest listed level, plus a status line |
| Model has no dial but a level was asked for | nothing, plus a status line |
| Background task (title, tag, follow-up) | nothing (provider default) |
The level in use is also reported in the per-reply toast (reasoning high) and on the dashboard's
Last request line.
Good to know about x-opencode-session
OpenCode's Go docs ask every client to “send a stable session ID in x-opencode-session for each
conversation so we can optimize routing and prompt caching”, and they mailed users that requests
missing the header may start erroring. The pipe does this:
SESSION_MODE |
What is sent | When to use |
|---|---|---|
chat (default) |
uuid5(namespace, "chat:<chat_id>") |
Correct per-conversation behaviour. Same chat → same id forever (survives restarts, no storage, regeneration/branching keeps the cache warm), new chat → new id. |
user |
uuid5(namespace, "user:<user_id>") |
Single-user setups where you want maximum cross-chat cache reuse. |
static |
STATIC_SESSION_ID |
Last-resort workaround if you ever need one fixed id. |
The header is also sent on GET /models and on auxiliary requests (title generation, tags, RAG
queries), which is exactly what the docs mean by “main and auxiliary requests”. Auxiliary calls
inherit the conversation's session id, so they share the same upstream route.
Usage & credits
Slash command — type any of /usage, /credits, /quota, /go, /allowance, /opencode in a
chat (any model) and the reply is a dashboard instead of a completion:
### 🟢 OpenCode Go — usage & credits
| Window | Used | Resets | Used (approx.) |
| :-- | :-- | :-- | :-- |
| 🟢 **5-hour** | `▓▓▓▓░░░░░░` 37% | in 4h 15m | ≈$4.44 / $12 |
| 🟠 **Weekly** | `▓▓▓▓▓▓░░░░` 62% | in 2d 18h | ≈$18.60 / $30 |
| 🔴 **Monthly** | `▓▓▓▓▓▓▓▓▓▓` 98% | in 19d 18h | ≈$58.80 / $60 |
> ⚠️ A limit is at or near its cap …
**Plan:** Go ($10/mo) · **Console:** https://opencode.ai/console/go
**Last request:** `kimi-k3` · 1,200 in / 300 out · 8,000 cache reads · allowance $0.0031 · list $0.00412
_Billing source: `balance` — you are past a Go limit and drawing on pay-as-you-go credit (Extra Usage)._
**This chat:** 3 requests · … · allowance $0.0093
Pseudo-model — OpenCode Go · Usage & credits in the model picker renders the same dashboard, for
people who prefer clicking to typing.
Per reply — every response carries usage.opencode.{cost, list_cost, allowance, billing_source, tokens}:
- forwarded as
stream_options.include_usageso Open WebUI's token counter and usage panel work; - summarised in a toast (
allowance $0.0031 · go), which you can switch off withUSAGE_TOAST; - accumulated per chat and globally in
$DATA_DIR/opencode-go-usage.json(TRACK_LOCAL_USAGE).
billing_source is the interesting one: go / go-plus means you are inside the subscription,
balance means Go is falling back to your pay-as-you-go credit, free means a free model.
Valves
| Valve | Default | Notes |
|---|---|---|
API_KEY |
(empty) | Shared Go key. Users can override with their own via User valves. |
BASE_URL |
https://opencode.ai/inference/go/openai/v1 |
OpenAI-compatible surface. Every text model works here — Console translates /responses, /messages and Gemini calls. |
PLAN |
go |
Label only: go, go-plus, go-teams. |
MONTHLY_LIMIT_USD |
0 |
Optional dollar basis for the dashboard (window caps are 20% / 50% / 100% of it). |
SESSION_MODE |
chat |
chat, user or static. |
STATIC_SESSION_ID |
(empty) | Only used by SESSION_MODE=static; auto-generated if empty. |
USER_AGENT |
open-webui-opencode-go/1.1.0 |
Clients should identify themselves instead of looking like a raw SDK. |
CLIENT_NAME |
open-webui |
Sent as x-opencode-client. |
EXPOSE_USAGE_MODEL |
true |
Adds the dashboard pseudo-model. |
INCLUDE_USAGE_IN_STREAM |
true |
stream_options.include_usage — needed for tokens/allowance. |
USAGE_TOAST |
true |
Toast after each reply. |
TRACK_LOCAL_USAGE |
true |
Persist per-chat / total counters. |
STATE_FILE |
$DATA_DIR/opencode-go-usage.json |
Where counters live. |
MODELS_URL / USAGE_URL |
(derived) | Override <origin>/inference/go/v1/{models,usage}. |
MODEL_CACHE_TTL |
300 |
Model-list cache, seconds. |
REQUEST_TIMEOUT |
0 |
0 = no total cap for streams (long agent runs). |
DEBUG_SESSION |
false |
Status line with the session id + billing summary. |
EXPOSE_REASONING_CONTROL |
true |
Publish a model_controls.reasoning_effort definition with each reasoning-capable model. |
REASONING_EFFORT |
(empty) | Floor for models with a dial when nobody picked a level. Empty = provider default. |
REASONING_EFFORT_LEVELS |
minimal, low, medium, high |
Levels offered when /models lists none. |
REASONING_MODELS |
(empty) | Extra model ids/globs that take reasoning_effort. |
NON_REASONING_MODELS |
(empty) | Model ids/globs that must never receive it (wins over everything). |
REASONING_EFFORT_OVERRIDES |
(empty) | glob=levels pins, entries separated by ;. |
Zero-code alternative (native OpenAI connection)
If you don't want a Pipe, you can still fix the header with a normal OpenAI connection, because
Open WebUI expands {{…}} placeholders in a connection's custom headers
(backend/open_webui/utils/headers.py):
- Base URL:
https://opencode.ai/inference/go/openai/v1 - API key: your
oc_sk_… - Headers:
{
"x-opencode-session": "{{CHAT_ID}}",
"x-opencode-client": "open-webui",
"User-Agent": "open-webui-opencode-go/1.1.0"
}
You get a proper per-chat session that way, but no usage/allowance or credit visibility —
stream_options.include_usage is dropped/rewritten by that path and there is no place to render it.
That is what this function adds.
Plan limits (from the Go docs, for reference)
Monthly allowance is per model and each model's usage counts against 5-hour (20%), weekly (50%) and
monthly (100%) windows of that model's limit. Token prices are identical across Go, Go Plus and Teams.
Example monthly limits on Go: GLM‑5.3‑Flash $60, Kimi K3 $15, Claude Haiku 5.5 $15, DeepSeek V4.1 Flash $100.
Per model usage within limits is reported as opencode.allowance — exactly what the toast shows.
Honest limitations
GET /inference/go/v1/usageis not in the documented API (the Go/Zen docs only say “track your usage in the console”). It returns percentages and reset times, and may change or vanish. The function treats failures as non-fatal and still shows per-request cost/allowance.- There is no public endpoint for the pay-as-you-go credit balance (feature request
anomalyco/opencode#10448). The dashboard therefore says so instead of inventing a number; use the console link, and watchbilling_source == "balance"to know you're drawing credit. - Go traffic is monitored for abuse. This function sends normal agent traffic with its own identity, but it does not make Open WebUI a “validated client”: if OpenCode changes the rules, only sending the headers covered here is guaranteed by this code.
- Model Controls are not renderable for pipe models on Open WebUI 0.12.3 / 0.13.x. The backend
rejects saving controls on a pipe-backed model and the frontend skips
pipemodels in the picker, so the knob will not appear there. The pipe publishes the control definition anyway (harmless, and live on releases that read it from a pipe) and does the same job through/reasoning. Its application side — reading a pick out ofparams.model_controls/metadata.params.model_controls— is fully wired, so nothing breaks if you switch to a release that does render them. - Whether a model takes
reasoning_effortis a guess when/modelssays nothing: the family heuristic can be wrong for a new or renamed model. Pin it withNON_REASONING_MODELS/REASONING_MODELS, or set the levels withREASONING_EFFORT_OVERRIDES.
Tests
pip install aiohttp
python tests/test_pipe.py
The harness spins up a fake Go endpoint and asserts: header identity and session stability (same chat →
same id, new chat → new id, user/static modes), payload hygiene (WebUI bookkeeping stripped, model
prefix stripped, include_usage set), streaming passthrough, usage capture/tracking, dashboard
rendering, /usage command and pseudo-model, non-streaming replies, the JSON-instead-of-SSE fallback,
429 error copy, missing key, and user-valve key precedence.
For reasoning effort it also covers: capability detection from every /models shape (level list,
supported_parameters, booleans, upstream opt-out, family heuristic, no-dial model), the published
control definition, a Model Controls pick from the metadata, a plain params.reasoning_effort, nearest-
level mapping with its status line, stripping for models without a dial, /reasoning list/set/reset and
its persistence, REASONING_MODELS / NON_REASONING_MODELS / REASONING_EFFORT_OVERRIDES /
REASONING_EFFORT / EXPOSE_REASONING_CONTROL, the background-task exemption, and the level showing up
in the toast and the dashboard. Exit code 0 = all good.