Skip to content

Commit 5b87f2c

Browse files
committed
feat(compaction): add configurable threshold and model (global + per-model)
Add compaction settings at both global and per-model levels: - threshold: trigger compaction at a percentage of context (1-100, default: 100) - model: use a different model for compaction summaries This allows users to: - Trigger compaction earlier for models that degrade at lower context usage - Use cheaper/faster models for compaction to save costs Closes anomalyco#11930 Relates to anomalyco#11314, anomalyco#8140, anomalyco#10017, anomalyco#8629
1 parent 0d22068 commit 5b87f2c

8 files changed

Lines changed: 136 additions & 5 deletions

File tree

‎packages/opencode/src/config/config.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,17 @@ export namespace Config {
909909
.record(
910910
z.string(),
911911
ModelsDev.Model.partial().extend({
912+
compaction_threshold: z
913+
.number()
914+
.int()
915+
.min(1)
916+
.max(100)
917+
.optional()
918+
.describe("Compact when context usage exceeds this percentage (1-100, overrides global threshold)"),
919+
compaction_model: z
920+
.string()
921+
.optional()
922+
.describe("Model to use for compaction (e.g., 'zhipu/glm-4-plus', overrides global compaction model)"),
912923
variants: z
913924
.record(
914925
z.string(),
@@ -1115,6 +1126,19 @@ export namespace Config {
11151126
.object({
11161127
auto: z.boolean().optional().describe("Enable automatic compaction when context is full (default: true)"),
11171128
prune: z.boolean().optional().describe("Enable pruning of old tool outputs (default: true)"),
1129+
threshold: z
1130+
.number()
1131+
.int()
1132+
.min(1)
1133+
.max(100)
1134+
.optional()
1135+
.describe(
1136+
"Default compaction threshold percentage (1-100). Compaction triggers when context usage exceeds this percentage (default: 100)",
1137+
),
1138+
model: z
1139+
.string()
1140+
.optional()
1141+
.describe("Default model to use for compaction (e.g., 'anthropic/claude-haiku-3-5-20241022')"),
11181142
})
11191143
.optional(),
11201144
experimental: z

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ export namespace Provider {
586586
headers: z.record(z.string(), z.string()),
587587
release_date: z.string(),
588588
variants: z.record(z.string(), z.record(z.string(), z.any())).optional(),
589+
compaction_threshold: z.number().int().min(1).max(100).optional(),
590+
compaction_model: z.string().optional(),
589591
})
590592
.meta({
591593
ref: "Model",
@@ -810,6 +812,8 @@ export namespace Provider {
810812
family: model.family ?? existingModel?.family ?? "",
811813
release_date: model.release_date ?? existingModel?.release_date ?? "",
812814
variants: {},
815+
compaction_threshold: model.compaction_threshold ?? existingModel?.compaction_threshold,
816+
compaction_model: model.compaction_model ?? existingModel?.compaction_model,
813817
}
814818
const merged = mergeDeep(ProviderTransform.variants(parsedModel), model.variants ?? {})
815819
parsedModel.variants = mapValues(

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { Config } from "@/config/config"
1818
export namespace SessionCompaction {
1919
const log = Log.create({ service: "session.compaction" })
2020

21+
export const THRESHOLD_DEFAULT = 100
22+
2123
export const Event = {
2224
Compacted: BusEvent.define(
2325
"session.compacted",
@@ -35,7 +37,8 @@ export namespace SessionCompaction {
3537
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
3638
const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
3739
const usable = input.model.limit.input || context - output
38-
return count > usable
40+
const threshold = input.model.compaction_threshold ?? config.compaction?.threshold ?? THRESHOLD_DEFAULT
41+
return count > usable * (threshold / 100)
3942
}
4043

4144
export const PRUNE_MINIMUM = 20_000
@@ -95,12 +98,18 @@ export namespace SessionCompaction {
9598
sessionID: string
9699
abort: AbortSignal
97100
auto: boolean
101+
model: Provider.Model
98102
}) {
103+
const config = await Config.get()
99104
const userMessage = input.messages.findLast((m) => m.info.id === input.parentID)!.info as MessageV2.User
100105
const agent = await Agent.get("compaction")
101-
const model = agent.model
102-
? await Provider.getModel(agent.model.providerID, agent.model.modelID)
103-
: await Provider.getModel(userMessage.model.providerID, userMessage.model.modelID)
106+
const compactionModelStr = input.model.compaction_model ?? config.compaction?.model
107+
const parsed = compactionModelStr ? Provider.parseModel(compactionModelStr) : undefined
108+
const model = parsed
109+
? await Provider.getModel(parsed.providerID, parsed.modelID)
110+
: agent.model
111+
? await Provider.getModel(agent.model.providerID, agent.model.modelID)
112+
: await Provider.getModel(userMessage.model.providerID, userMessage.model.modelID)
104113
const msg = (await Session.updateMessage({
105114
id: Identifier.ascending("message"),
106115
role: "assistant",

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ export namespace SessionPrompt {
495495
abort,
496496
sessionID,
497497
auto: task.auto,
498+
model,
498499
})
499500
if (result === "stop") break
500501
continue

‎packages/sdk/js/src/v2/gen/types.gen.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,14 @@ export type ProviderConfig = {
15001500
[key: string]: unknown | boolean | undefined
15011501
}
15021502
}
1503+
/**
1504+
* Compact when context usage exceeds this percentage (1-100, overrides global threshold)
1505+
*/
1506+
compaction_threshold?: number
1507+
/**
1508+
* Model to use for compaction (e.g., 'zhipu/glm-4-plus', overrides global compaction model)
1509+
*/
1510+
compaction_model?: string
15031511
}
15041512
}
15051513
whitelist?: Array<string>
@@ -1786,6 +1794,14 @@ export type Config = {
17861794
* Enable pruning of old tool outputs (default: true)
17871795
*/
17881796
prune?: boolean
1797+
/**
1798+
* Default compaction threshold percentage (1-100). Compaction triggers when context usage exceeds this percentage (default: 100)
1799+
*/
1800+
threshold?: number
1801+
/**
1802+
* Default model to use for compaction (e.g., 'anthropic/claude-haiku-3-5-20241022')
1803+
*/
1804+
model?: string
17891805
}
17901806
experimental?: {
17911807
disable_paste_summary?: boolean
@@ -1918,6 +1934,8 @@ export type Model = {
19181934
[key: string]: unknown
19191935
}
19201936
}
1937+
compaction_threshold?: number
1938+
compaction_model?: string
19211939
}
19221940

19231941
export type Provider = {

‎packages/sdk/openapi.json‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9284,6 +9284,16 @@
92849284
},
92859285
"additionalProperties": {}
92869286
}
9287+
},
9288+
"compaction_threshold": {
9289+
"description": "Compact when context usage exceeds this percentage (1-100, overrides global threshold)",
9290+
"type": "integer",
9291+
"minimum": 1,
9292+
"maximum": 100
9293+
},
9294+
"compaction_model": {
9295+
"description": "Model to use for compaction (e.g., 'zhipu/glm-4-plus', overrides global compaction model)",
9296+
"type": "string"
92879297
}
92889298
}
92899299
}
@@ -9843,6 +9853,16 @@
98439853
"prune": {
98449854
"description": "Enable pruning of old tool outputs (default: true)",
98459855
"type": "boolean"
9856+
},
9857+
"threshold": {
9858+
"description": "Default compaction threshold percentage (1-100). Compaction triggers when context usage exceeds this percentage (default: 100)",
9859+
"type": "integer",
9860+
"minimum": 1,
9861+
"maximum": 100
9862+
},
9863+
"model": {
9864+
"description": "Default model to use for compaction (e.g., 'anthropic/claude-haiku-3-5-20241022')",
9865+
"type": "string"
98469866
}
98479867
}
98489868
},
@@ -10194,6 +10214,14 @@
1019410214
},
1019510215
"additionalProperties": {}
1019610216
}
10217+
},
10218+
"compaction_threshold": {
10219+
"type": "integer",
10220+
"minimum": 1,
10221+
"maximum": 100
10222+
},
10223+
"compaction_model": {
10224+
"type": "string"
1019710225
}
1019810226
},
1019910227
"required": [

‎packages/web/src/content/docs/config.mdx‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,37 @@ You can control context compaction behavior through the `compaction` option.
488488
"$schema": "https://opencode.ai/config.json",
489489
"compaction": {
490490
"auto": true,
491-
"prune": true
491+
"prune": true,
492+
"threshold": 80,
493+
"model": "anthropic/claude-haiku-3-5-20241022"
492494
}
493495
}
494496
```
495497

496498
- `auto` - Automatically compact the session when context is full (default: `true`).
497499
- `prune` - Remove old tool outputs to save tokens (default: `true`).
500+
- `threshold` - Percentage of context usage that triggers compaction, from 1-100 (default: `100`). Lower values trigger compaction earlier, which can help maintain model performance in long sessions.
501+
- `model` - Model to use for generating the compaction summary. Defaults to the session's current model.
502+
503+
You can also configure compaction per-model. This is useful when different models have different optimal context thresholds.
504+
505+
```json title="opencode.json"
506+
{
507+
"$schema": "https://opencode.ai/config.json",
508+
"provider": {
509+
"anthropic": {
510+
"models": {
511+
"claude-opus-4-20250514": {
512+
"compaction_threshold": 50,
513+
"compaction_model": "anthropic/claude-haiku-3-5-20241022"
514+
}
515+
}
516+
}
517+
}
518+
}
519+
```
520+
521+
Per-model settings override the global `compaction` config. This lets you use different thresholds and models for each provider/model combination.
498522

499523
---
500524

‎packages/web/src/content/docs/models.mdx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ The built-in provider and model names can be found on [Models.dev](https://model
105105

106106
You can also configure these options for any agents that you are using. The agent config overrides any global options here. [Learn more](/docs/agents/#additional).
107107

108+
You can also configure per-model compaction settings. This is useful when different models perform better with earlier compaction thresholds.
109+
110+
```jsonc title="opencode.jsonc" {7-8}
111+
{
112+
"$schema": "https://opencode.ai/config.json",
113+
"provider": {
114+
"anthropic": {
115+
"models": {
116+
"claude-opus-4-20250514": {
117+
"compaction_threshold": 50,
118+
"compaction_model": "anthropic/claude-haiku-3-5-20241022",
119+
},
120+
},
121+
},
122+
},
123+
}
124+
```
125+
126+
- `compaction_threshold` - Percentage of context (1-100) that triggers compaction. Overrides the global setting.
127+
- `compaction_model` - Model to use for compaction. Useful for using a cheaper/faster model.
128+
129+
[Learn more about compaction](/docs/config#compaction).
130+
108131
You can also define custom variants that extend built-in ones. Variants let you configure different settings for the same model without creating duplicate entries:
109132

110133
```jsonc title="opencode.jsonc" {6-21}

0 commit comments

Comments
 (0)