Token use
100 OpenCode compactions: when they fire, what is left
On my own OpenCode database, automatic compaction fires near 250k tokens and leaves about a fifth. Queries to run on yours, and what to set.
Kifah Abbad Published Last verified 8 min read
The short answer. On my own OpenCode database, automatic compaction fired at about 247,000 tokens of context and left about 51,000; manual ones fired at about 411,000 and left about 81,000. Four queries below show the same numbers for your sessions, and what to set to compact earlier.

Why this matters. A long session slowly fills the model’s context window. Once it is full or crowded, the agent gets slower, costs more per message and starts to lose track of earlier decisions. Compaction replaces the older part of the conversation with a summary, and it is how you keep a clean, working context without starting over. The useful question is when to do it: too late and the session drifts, too early and you throw away detail you still need.
What you get. A way to see, from your own OpenCode database, when compaction actually fired on your sessions and how much context was left afterwards, so you can decide whether to compact earlier or by hand at the end of a task, and which setting to change.
A real session (my data, content not read). Among my sessions with at least 40 messages and a compaction, the first compaction came after a median of 41 percent of the assistant’s messages (10th to 90th percentile: 14 to 82 percent, 43 sessions). In the 17 compacted sessions with 80 to 300 messages, an average session held 28 of my own messages and 167 from the assistant. So compaction is not an end-of-session event: in many of my sessions it landed with more than half of the work still ahead. That is the point of choosing the moment.
I use my own history as the sample: 100 compactions in 45 sessions, 63 triggered automatically near the limit and 37 by me. Automatic ones fired when the context was about 247,000 tokens and left about 51,000, a fifth. Manual ones fired later, at about 411,000, and left about 81,000. Below are the four queries, so you can get the same numbers for your own sessions, and what I would change after reading mine.
Every claim carries a label: tested (run on a copy of my database on 19 November 2026, OpenCode 2.3.0, sqlite3 3.50.6), estimated, assumed, or not tested. I read counts and token totals, never message content. What each agent keeps and how to set it is on the compaction reference page, which this post does not repeat.
Who needs this
Possible scenario. You pick up a ticket in Jira. You and the agent go back and forth: it reads the code, changes it, runs the tests, you correct it, it fixes what broke. By the hundredth message the window holds finished work, old test output and a dead end you already dropped. The agent compacts in the middle of the next bug, and the summary decides what it still remembers about your decisions. Whether that happens at a good moment or a bad one is not luck: it depends on when you compact and what you set.
| Role | What changes for them |
|---|---|
| Junior software engineer | Knows that a sudden “forgetful” agent is often a compaction, not a mistake of theirs, and can compact at a task boundary instead. |
| Architect | Sees which design decisions survive a compaction and which exist only in the chat, and puts the ones that matter in instruction files. |
| DevOps or platform engineer | Gets a query that shows when compaction fires per model, and one setting (compaction.buffer) to tune for the whole team. |
Table: who gains what, my reasoning, not measured. I did not measure time or cost saved for any role.
The problem: you do not know when it will happen, or what it costs you
A long session ends in a compaction you did not choose, at a moment you did not choose, and afterwards the model has a summary instead of the conversation. I wanted to know three things about my own use: when it fires, how much is left afterwards, and whether the history is still there.
What my 100 compactions look like
SELECT json_extract(data, '$.reason') AS reason,
count(*) AS compactions,
count(DISTINCT session_id) AS sessions
FROM session_message
WHERE type = 'compaction'
GROUP BY reason;
reason compactions sessions
------ ----------- --------
auto 63 33
manual 37 19
Compactions by reason. Seven of the 45 sessions have both kinds, so the session counts add up to more than 45.
I measure the size of the context by the last assistant message before the compaction and the first one after it: its input tokens plus the cache read and cache write tokens.
WITH a AS (
SELECT session_id, time_created AS t,
json_extract(data, '$.tokens.input')
+ coalesce(json_extract(data, '$.tokens.cache.read'), 0)
+ coalesce(json_extract(data, '$.tokens.cache.write'), 0) AS size
FROM session_message
WHERE type = 'assistant' AND json_extract(data, '$.tokens.input') > 0
), c AS (
SELECT session_id, time_created AS t,
json_extract(data, '$.reason') AS reason
FROM session_message WHERE type = 'compaction'
)
SELECT reason, count(*) AS n,
CAST(avg((SELECT size FROM a WHERE a.session_id = c.session_id
AND a.t < c.t ORDER BY a.t DESC LIMIT 1)) AS INT) AS before,
CAST(avg((SELECT size FROM a WHERE a.session_id = c.session_id
AND a.t > c.t ORDER BY a.t LIMIT 1)) AS INT) AS after
FROM c GROUP BY reason;
reason n before after
------ -- ------ -----
auto 63 247498 51245
manual 37 410820 80996
Average context size, in tokens, just before (before) and just after (after) a compaction.
Dividing the two averages gives 21 percent for automatic ones (51,245 of 247,498) and 20 percent for manual ones (80,996 of 410,820). Those percentages are my arithmetic on the averages, not a separate measurement. That is a measurement of size, not of quality: I did not read any summary, so I cannot say what was lost.
The history is not lost on disk:
SELECT count(DISTINCT session_id) AS sessions,
sum(type IN ('user', 'assistant')) AS messages_still_stored
FROM session_message
WHERE session_id IN (SELECT session_id FROM session_message
WHERE type = 'compaction');
sessions messages_still_stored
-------- ---------------------
45 22102
Messages still in the table for the sessions that have a compaction. OpenCode’s documentation says earlier messages remain stored even when they are no longer sent to the model.
It depends on the model
WITH c AS (
SELECT session_id, time_created AS t
FROM session_message WHERE type = 'compaction'
), last AS (
SELECT
(SELECT json_extract(m.data, '$.model.id') FROM session_message m
WHERE m.session_id = c.session_id AND m.type = 'assistant'
AND m.time_created < c.t ORDER BY m.time_created DESC LIMIT 1) AS model,
(SELECT json_extract(m.data, '$.tokens.input')
+ coalesce(json_extract(m.data, '$.tokens.cache.read'), 0)
+ coalesce(json_extract(m.data, '$.tokens.cache.write'), 0)
FROM session_message m
WHERE m.session_id = c.session_id AND m.type = 'assistant'
AND m.time_created < c.t ORDER BY m.time_created DESC LIMIT 1) AS size
FROM c
)
SELECT model, count(*) AS n, CAST(avg(size) AS INT) AS avg_context
FROM last GROUP BY model HAVING n >= 4 ORDER BY n DESC;
model n avg_context
---------------------------------------------------- -- -----------
claude-opus-4-8 42 213838
claude-opus-5 20 364477
DeepSeek-V4-Pro-Qwen3.5-9B-UNCENSORED-heretic-Q4_K_M 10 16582
claude-sonnet-5-5 7 451267
claude-sonnet-5 5 75742
deepseek-v4-flash-free 4 278198
Compactions and the average context size at the trigger, by the model that wrote the last message before it.
The numbers do not form one rule, because the windows differ. My configuration lists a 1,000,000-token window for claude-sonnet-5-5 and a 65,536-token window for the local model in the table, so a different trigger is expected. The local model’s average of 16,600 is low even for a 64k window, and I did not investigate why. My own setting is buffer: 10%, which keeps a tenth of the window free, so on a 1M window I would expect an automatic compaction around 900,000, and none of the Claude averages in this table is near that. These are averages, and manual compactions are in the same table, so this does not prove automatic ones fire early. I did not split the table by reason, and I did not find out why the numbers are lower.
The solutions that exist today
All three agents compact on a command and automatically near the limit; the reference page has the settings. For OpenCode on 2.3.0:
- Compact earlier. Raise
compaction.buffer(it is the share of the window kept free, default 15 percent). I did not test the effect on my sessions. - Keep more recent detail. Raise
compaction.keep.tokens(default 15,000). Not tested. - Compact yourself, at a task boundary.
opencode api post /api/session/<id>/compact --data '{}'. The reply says the request was accepted; it does not wait for the summary. Tested earlier on a throwaway session.
What I would change, and what I will not claim
My manual compactions ran at about 1.7 times the size of my automatic ones (411,000 against 247,000 tokens). My guess is that I compacted by hand at the end of a long task and let the automatic one fire in the middle of a long run, but I have not checked that, and the two groups are different sessions. I would try compacting at the end of each task and see whether the context after it stays smaller. I did not measure whether this saves tokens or time.
Two numbers that look like savings are not:
- The 42 sessions with a compaction (and five or more messages) average 541 messages and about $4.31; the 112 without one average 96 messages and $0.09. That says long sessions cost more, not that compaction costs anything.
- A compaction leaves about a fifth of the context, but the next messages are billed on that smaller context, and I did not compare the cost per message before and after.
Ten minutes: see yours
Make a copy with SQLite’s own backup command, which is safe while OpenCode is open, then paste each query from this post into the sqlite3 prompt:
mkdir -p /tmp/oc
sqlite3 ~/.local/share/opencode/opencode.db ".backup '/tmp/oc/opencode.db'"
sqlite3 -readonly -header -column /tmp/oc/opencode.db
Delete /tmp/oc when you are done: the copy holds your whole history.
If you have fewer than ten compactions, the averages mean little. If the before column sits far below your model’s window, compaction is firing early, and compaction.buffer is the setting to look at. If your OpenCode is not 2.x, there is no session_message table and the queries will fail.
If you want a compaction summary as a readable file, the export post shows the filter that keeps it (the compaction message is one of the types it writes).
What this post does not cover
- Claude Code and Codex. I did not measure either. The Codex rollouts on this machine hold three compactions, too few to say anything.
- Summary quality. I never read a summary.
- Cost. I did not compare the cost per message before and after.
- One person’s sessions. 100 compactions in 45 sessions on one Mac, with several models, one of them local.
- Other OpenCode versions. Everything is 2.3.0. The query relies on the
session_messagecolumns of 2.x. - Windows and Linux. The
mkdirand.backuplines ran on macOS and, on a small database, on Ubuntu 24.04 in a container on 20 November 2026 (tested);/tmpis a Unix path, so on Windows use another folder andsqlite-tools-win-x64from sqlite.org’s download page (documented). Nothing ran on Windows, and I did not run OpenCode on Linux.