opencode-context-watch
v1.0.7
Published
OpenCode V2 plugin to warn when a session's context window usage crosses a configurable threshold
Readme
opencode-context-watch
Warn an OpenCode agent before its context window fills, so it can wrap up or compact in time.
[!warning] This plugin targets OpenCode 2.x only. On OpenCode 1.x it is a silent no-op: there is no V1 support and no fallback path. See ADR-004; the compaction tool API is verified on OpenCode 2.3.0.
Installation
Add the package to opencode.json. Opencode hands the options object to the
plugin, so this one edit is the whole install:
{
"plugins": [
{
"package": "/path/to/opencode-context-watch",
"options": {
"warnPercent": 0.7
}
}
]
}package takes the published package name, or a path to a clone or symlink of
this repository while you develop it. Restart opencode after editing
opencode.json. Nothing loads on OpenCode 1.x.
Usage
There is no command to run. Once loaded, the plugin watches every session and on each model request whose context usage is over a threshold it adds a warning to the conversation:
[context-watch] Context 78% (102,400/128,000 tokens) is above this session's threshold.
1. Persist only what your workflow requires: scratch space, memory, or task notes. Do not create a git commit unless the task asks for one.
2. Call the compact_context tool with one continuation message describing how to resume the task from the summary.The agent reads that as part of the conversation, saves any required notes, then
calls the plugin's compact_context tool. The tool compacts that session and
posts the message as a steer prompt after the summary. Set options to taste,
then restart opencode.
Sidebar
In the TUI the plugin adds a Context Watch section to the sidebar for the
session on screen: usage against the model window, the band that will warn, and
whether a compaction is running. The title turns yellow over the band. It reads
its own context-watch.status RPC and follows the plugin's changed event, so
it needs no other plugin installed.
/context-watch toggles the section.
Options
| Option | Default | Description |
|--------|---------|-------------|
| warnPercent | 0.77 | 0..1, or percent (e.g. 77) if > 1. Warn when usage reaches this fraction of the model window. null disables this band |
| warnTokens | 150000 | Warn when the session reaches this many tokens. null disables this band |
| windowTokens | — | Override the model's context window |
| rearmPercent | 5 | Log again after this many percentage-point rise |
| rearmTokens | 5000 | Log again after this many more tokens |
| verbose | false | Log every context warning to the service log |
| message | — | Template with the placeholders {usage}, {percent}, {tokens}, {window}; the default tells the agent to call compact_context |
Either band can fire first, and either can be disabled with null. The percent
band needs a known window; without one only the token band applies and {usage}
renders the token count alone. {percent} and {window} still render as 0 and
unknown when the window is unavailable.
A bad value falls back to its default and the other options still apply. The
problem is written to the OpenCode service log as
[context-watch] <key>: <what is wrong>. Headless opencode2 run --print-logs
does not print plugin console output, so those lines only show up in a service
log or a terminal run.
How it works
- On load the plugin reads the model list and remembers each model's context window.
- It listens for
session.step.endedand keeps the token usage of the last completed step per session. That sample —input + output + reasoning + cache.read + cache.write— is the number opencode's own context meter shows. Cumulativesession.usage.updatedevents are ignored on purpose. - On every model request over a threshold it appends a synthetic user message to that request. The message is never stored, which is why it is added on every above-threshold request rather than only the first.
- It registers a
compact_contexttool when the host supports tool registration. The tool takes onlymessage; it reads the calling session from the tool context, admits a compaction withsession.compact, then posts the text as a steer prompt withsession.prompt. Conflicts return a short failure result. - It listens for
session.compaction.started,session.compaction.ended, andsession.compaction.failed. Warnings are suppressed while compaction runs, and the pre-compaction sample and rearm state are dropped when it ends or fails, so the first post-compaction request waits for a fresh sample. - It drops the per-session sample, rearm state, and compacting mark on
session.deleted. - It registers a read-only
context-watchRPC when the host hasctx.rpc.register:status({ sessionID })returns the sample, the model window, the active bands, and the compacting flag, andchangedfires on every completed step and compaction transition. The window comes from the session's model record when the instance answering never ran the context hook. The report plugin's sidebar reads it; hosts without RPC registration keep the warning path unchanged. rearmPercentandrearmTokensonly limit how often the service log repeats the warning, never the warning itself.
Two limits worth knowing:
- The first one or two model requests of a session cannot warn. OpenCode publishes a step's token usage after the next request has already been assembled, so a cold start has no sample yet. After that every above-threshold request is warned. The first request after a compaction also cannot warn, because the pre-compaction sample is dropped. That is fine for a "wrap up soon" hint, and it is why the plugin does not ask OpenCode to re-serialize the transcript on every request.
- OpenCode performs the compaction; the plugin only admits it. The continuation text is a durable steer message after the checkpoint, unlike the transient warning.
Docs
- Design and Technical Reference — runtime contracts, event ordering, the threshold model, and error handling
- Architecture decisions — the ADR series, including why the plugin is V2-only
- Agent and development guide — build commands,
./dev.sh all, and the verified OpenCode runtime facts - Commit and changelog conventions — how commits turn into release notes
License
MIT
