npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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.

npm Release License

[!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.ended and 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. Cumulative session.usage.updated events 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_context tool when the host supports tool registration. The tool takes only message; it reads the calling session from the tool context, admits a compaction with session.compact, then posts the text as a steer prompt with session.prompt. Conflicts return a short failure result.
  • It listens for session.compaction.started, session.compaction.ended, and session.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-watch RPC when the host has ctx.rpc.register: status({ sessionID }) returns the sample, the model window, the active bands, and the compacting flag, and changed fires 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.
  • rearmPercent and rearmTokens only 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

License

MIT