Optimize OpenCode: How to lower your API costs with RTK, DCP and OpenSlimedit

The question of how to embed AI appropriately into the own development environment without losing control of code and data has been busy for a while. On the topic sovereign development with AI I have already written a lot. With OpenCode we follow this path consistently – and three plugins also make use significantly cheaper.

What is OpenCode?

OpenCode is an open source AI coding agent that runs directly in the terminal. While Cursor or GitHub copilot are proprietary systems that send their own code to US servers, OpenCode can also be operated with local models. In addition, the tool can be configured with more than 70 other models, including some free models (which then use the inputs for training). This is not a small print, but an architectural decision.

Those who rely on European hosting – for example using IONOS, STACKIT or the Open Telekom Cloud – can configure OpenCode easily to point to the corresponding OpenAI compatible endpoints. Hence, the own engineering can stay independent of the large US platforms. No lock-in, no forced dependence on a single model.

The installation is simple:

# macOS/Linux
curl -fsSL https://opencode.ai/install | bash

# oder via Homebrew
brew install anomalyco/tap/opencode

The Token Maxxing problem

Sovereignty can, however, take a turn that quickly makes itself financially noticeable: Those who pay for requests to a language model, i.e. does not use a subscription like Claude Pro but call the API directly – be it with IONOS, OpenAI or another provider – are paying per token. And those disappear faster than thought.

Ein einzelner „npm test“-Lauf auf einem mittelgroßen Projekt produziert schnell 200 Zeilen Ausgabe. Der Großteil davon sind bestandene Tests, die das Modell eigentlich nicht braucht. „kubectl get pods“ in einem Cluster mit ein paar Dutzend Services liefert eine lange Statustabelle, von der das Modell in den meisten Fällen nur zwei oder drei Zeilen wirklich braucht. Multipliziert man das mit einer längeren Arbeitsession, läuft das Kontextfenster schnell voll – und die Rechnung entsprechend hoch. Ein Caching with litellm can help, but is also not a quick fix.

The three plugins I present in the following attack at different points: RTK and opencode-snip compress the output of individual commands before they reach the context of the model at all. OpenSlimedit reduces the tool descriptions that are sent with each API call. DCP clears what has accumulated in the context.

RTK – Rust Token Killer

RTK is a CLI proxy that sits between the shell and the language model. When OpenCode executes a command, RTK captures the output, compresses it and only forwards the relevant information to the model. The model sees a clean, compact edition – without any changes in the workflow. In my experience, this also works quite well, interestingly, the model has complained more than once about RTK being a proxy and not providing full information. In these cases, Python was used to get unfiltered to the information. Because RTK supports only a certain number of functions. The exact list can be found in the RTK documentation.

Was der Eingriff konkret bedeutet, zeigt ein direkter Vergleich. Ohne RTK liefert „kubectl get pods -n production“ in einem Cluster mit mehreren Diensten beispielsweise folgende Ausgabe:

NAME                                        READY   STATUS    RESTARTS   AGE
api-gateway-7d4f8b9c6-xk2pq                 1/1     Running   0          3d
api-gateway-7d4f8b9c6-zr9lm                 1/1     Running   0          3d
auth-service-6c8d7f5b4-jn3ks                1/1     Running   0          5d
auth-service-6c8d7f5b4-wt7qp                1/1     Running   2          5d
frontend-deployment-5b9c8d7f6-hm4xr         1/1     Running   0          1d
frontend-deployment-5b9c8d7f6-pv6ns         1/1     Running   0          1d
notification-worker-4a7b6c5d3-ck8yt         0/1     Pending   0          12m
postgres-statefulset-0                      1/1     Running   0          10d
postgres-statefulset-1                      1/1     Running   0          10d
redis-deployment-3f6e5d4c2-bq5wr            1/1     Running   0          7d

With RTK the model instead receives:

pods: 9/10 Running | 1 Pending: notification-worker-4a7b6c5d3-ck8yt (12m)
restarts: auth-service-6c8d7f5b4-wt7qp (2)

Semantically identical but using about 75% less tokens. The model's agentic loop delivers the same results – only cheaper.

Installation

# macOS/Linux – Homebrew empfohlen
brew install rtk-ai/tap/rtk

# oder per curl
curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/master/install.sh | sh

Then integrate RTK as a plugin into OpenCode:

# Plugin installieren – legt rtk.ts direkt in ~/.config/opencode/plugins/ ab
rtk init -g --opencode

# Verifizieren
rtk gain

If you do not want to install a separate binary, you can use opencode-snip instead. It works according to the same principle – CLI outputs are compressed before they see the model – but requires only npm and no additional system binary:

npm install opencode-snip
{
  "plugin": ["opencode-snip"]
}

OpenSlimedit – less overhead at any API call

OpenSlimedit reduces aggressively all tool descriptions and compresses read outputs. As tool templates are sent to each API call, there are thousands of input tokens that can be saved – regardless of what the command itself delivers.

This is an approach different from what RTK and DCP (more than that) promise. OpenCode knows a number of built-in tools – read, write, search, execute shell commands. Each of these tools brings a description that explains to the model what it can do and how to call it. These descriptions land in the prompt for each single API call – even if the model does not need the most tools in this step. OpenSlimedit shortens these descriptions as far as possible without the model losing functionality. According to the benchmarks of the project, this results in a savings of up to 45% – without any configuration effort.

Installation

npm install openslimedit
{
  "plugin": ["openslimedit"]
}

DCP – Dynamic Context Pruning

RTK and OpenSlimedit solve the problem at the level of individual commands and API calls. DCP solves another problem: What happens to the context that has accumulated over a long session?

Each coding agent reads files, executes commands, analyzes errors. After an hour of work, there is still the complete content of a "package.json" in the context window, which the model has read twenty steps ago and no longer needs. DCP recognizes such outdated tool outputs and replaces them with placeholders before the context is sent to the model. The session history remains unaffected – DCP only changes what the model actually sees.

A concrete example: At the beginning of the session, the model has read "src/api/routes.ts" to understand the existing routing structure. Several refactoring steps later – new endpoints added, existing rebuilt, file read in again – the original read result is still in the context. Outdated, useless, expensive. DCP replaces it with:

[Output removed to save context - information superseded or no longer needed]

What was before ~800 tokens are now 12. The model continues to work without seeing the outdated information.

Noch deutlicher wird der Effekt bei einem typischen Node.js-Debugging-Szenario: Das Modell führt „npm test“ aus, ein Test schlägt fehl, der vollständige Testlauf mit allen Stack-Traces landet im Kontext. Das Modell korrigiert den Fehler und führt die Tests erneut aus – diesmal erfolgreich. Der ursprüngliche, fehlerhafte Testlauf hängt jetzt noch im Kontext und belegt Platz, den das Modell nicht mehr braucht. DCP entfernt nach konfigurierbaren vier Durchläufen den veralteten Output automatisch. Der Kontext bleibt informativ, ohne aufgebläht zu werden.

Installation

npm install -g @tarquinen/opencode-dcp
// ~/.config/opencode/opencode.json
{
  "plugin": ["@tarquinen/opencode-dcp@latest"]
}

Restart OpenCode – DCP then runs automatically in the background. Slash-Commands can be used to interact with the tool:

/dcp compress          # Kontext sofort komprimieren
/dcp decompress 2      # Komprimierung Nr. 2 rückgängig machen
/dcp stats             # Token-Statistiken der Session anzeigen

Three plugins, one goal

The three plugins attack different levels and do not interfere with each other. RTK or opencode-snip prevent too much noise coming into the context at all. OpenSlimedit reduces the overhead created by tool descriptions for each API call. DCP clears what has accumulated anyway. Together, they significantly extend the usable session time – and reduce costs per session accordingly.

Anyone who runs OpenCode with an European model provider such as IONOS and pays per token will notice this directly in the billing. And if you use a flat rate model, you hit your rate limits less frequently.

Two considerations remain: DCP invalidates the prompt caching at Anthropic and OpenAI through its interventions, or even in combination with litellm. If you heavily rely on cache hits, this might warrant not using DCP at all. In longer sessions, the savings IMHO still outweigh the savings. RTK also only handles Bash tool cases – built-in tools like direct file readings bypass the hook. In these cases, DCP is all the more important.

A subject that is deliberately excluded in this article is persistent memory – the ability of OpenCode to keep knowledge across sessions. There are also interesting plugins for this. More in the next post.