Skip to content

How to Configure Auto-Compact Settings in OpenCode for GLM5 - Context Limit Guide

Problem

I was using GLM5 with OpenCode and kept hitting context overflow errors. The conversation would grow, and suddenly I’d get an error like this:

Context length exceeded: The conversation has grown beyond the model's context window.
Please reduce the context or enable auto-compaction.

My opencode.json configuration was minimal - I hadn’t set any context limits:

opencode.json (BEFORE)
{
"models": {
"default": "glm-5"
}
}

When I checked the token usage, I realized GLM5 has a context window of around 128,000 tokens, but OpenCode wasn’t managing it properly. The model would try to process everything in the conversation history until it failed.

What happened?

I searched for solutions and found a Reddit discussion about configuring OpenCode for different models. The key insight was that OpenCode needs explicit instructions on how to manage context for each model.

The problem had two causes:

Cause 1: No context limit specified

OpenCode didn’t know GLM5’s limits, so it would let the conversation grow unchecked. When the context exceeded what GLM5 could handle, the API would reject the request.

Cause 2: No auto-compaction enabled

Even if I specified limits, OpenCode wouldn’t automatically trim old context unless I told it to. The conversation history would keep accumulating until it hit the wall.

How to solve it?

I found two methods to solve this problem. Both work, but they approach the issue differently.

Method 1: Model-specific context limits

The first approach is to set explicit context and output limits in your opencode.json file under the specific model configuration:

opencode.json
{
"models": {
"default": "glm-5",
"glm-5": {
"context": 95000,
"output": 8192
}
}
}

The key settings here:

  • context: 95000 - This is slightly under GLM5’s maximum context window. I use 95,000 instead of 128,000 to leave room for the response and avoid edge cases where the actual limit might be slightly different.
  • output: 8192 - This tells OpenCode the maximum tokens GLM5 can generate in a single response.

I tested this configuration with a long coding session:

Session started with 0 tokens
After 5 exchanges: ~15,000 tokens
After 10 exchanges: ~32,000 tokens
After 20 exchanges: ~78,000 tokens
After 25 exchanges: ~94,000 tokens - auto-compact triggered
Context trimmed to ~45,000 tokens
Session continued without errors

The auto-compaction kicked in when the context approached my 95,000 limit, and it automatically trimmed older messages while preserving recent context.

Method 2: Global compaction settings

The second approach uses a dedicated compaction block with more granular control:

opencode.json
{
"models": {
"default": "glm-5"
},
"compaction": {
"auto": true,
"prune": true,
"reserved": 20000
}
}

What each setting does:

  • auto: true - Enables automatic compaction when context grows too large
  • prune: true - Removes less important messages instead of summarizing them
  • reserved: 20000 - Keeps 20,000 tokens of recent context when compaction triggers

This method works across all models, not just GLM5. I tested it with both GLM5 and Claude:

Testing compaction with reserved: 20000
Model: GLM5
Initial context: 0 tokens
After many exchanges: 90,000+ tokens
Compaction triggered, trimmed to: ~20,000 tokens
Recent messages preserved: Yes
Session continued: Successfully

Which method should I use?

After testing both, I found:

Use Method 1 (model-specific limits) when:

  • You want per-model control
  • Different models have different context windows
  • You prefer explicit limits over automatic management

Use Method 2 (global compaction) when:

  • You use multiple models with similar limits
  • You want simpler configuration
  • You prefer automatic context management

You can also combine both:

opencode.json
{
"models": {
"default": "glm-5",
"glm-5": {
"context": 95000,
"output": 8192
},
"claude-3-5-sonnet": {
"context": 180000,
"output": 8192
}
},
"compaction": {
"auto": true,
"prune": true,
"reserved": 20000
}
}

This hybrid approach gives you model-specific limits plus automatic compaction behavior.

The reason

Understanding why this works requires knowing how OpenCode manages context:

Context window basics

Every model has a maximum context window - the total number of tokens it can process in one request. This includes:

  • System prompts
  • Conversation history
  • User messages
  • The model’s response (output tokens)

For GLM5, this limit is around 128,000 tokens total. But the usable limit is lower because:

  • Some tokens are reserved for the system prompt
  • Output tokens count against the limit
  • Safety margins prevent edge-case failures

Why 95,000 instead of 128,000?

Total GLM5 context window: 128,000 tokens
- System prompt overhead: ~1,000 tokens
- Output buffer (8,192): ~8,000 tokens
- Safety margin: ~24,000 tokens
= Practical limit: ~95,000 tokens for input

Setting the context limit at 95,000 leaves room for:

  • The model to generate responses without truncation
  • System messages that OpenCode adds automatically
  • Any overhead from the API layer

How auto-compaction works

When auto-compaction is enabled, OpenCode monitors the token count:

Token count: 75,000 → Continue normally
Token count: 85,000 → Continue normally
Token count: 94,000 → Compaction threshold reached
Compaction triggered → Trim older messages
Token count: 45,000 → Continue with trimmed context

The reserved setting determines how much context survives the compaction:

  • Low reserved (5,000): Very aggressive trimming, only recent messages survive
  • Medium reserved (20,000): Balanced, keeps recent exchanges plus context
  • High reserved (50,000): Conservative, keeps most of the conversation

Common mistakes

While setting this up, I made several mistakes that you should avoid:

Mistake 1: Setting limits too high

WRONG
{
"models": {
"glm-5": {
"context": 128000
}
}
}

This seems logical (use the full context window), but it fails because:

  • No room for output tokens
  • No buffer for API overhead
  • Edge cases cause failures

Mistake 2: Ignoring output limits

WRONG
{
"models": {
"glm-5": {
"context": 95000
}
}
}

Without output specified, OpenCode doesn’t know how much to reserve for the response. The model might try to generate 16,000 tokens and fail.

Mistake 3: Not enabling auto-compaction

WRONG
{
"models": {
"glm-5": {
"context": 95000,
"output": 8192
}
}
// Missing: compaction settings
}

This works until you hit the limit, then you’re stuck manually clearing context. Auto-compaction prevents this.

Mistake 4: Wrong configuration location

WRONG
{
"context": 95000,
"output": 8192,
"models": {
"default": "glm-5"
}
}

The limits need to be inside the model configuration, not at the root level. This mistake caused my settings to be completely ignored.

Mistake 5: Using only one method

Both methods work, but using only one limits flexibility. The combination approach gives you the best of both:

RECOMMENDED
{
"models": {
"default": "glm-5",
"glm-5": {
"context": 95000,
"output": 8192
}
},
"compaction": {
"auto": true,
"prune": true,
"reserved": 20000
}
}

Summary

In this post, I showed how to configure auto-compact settings in OpenCode for GLM5 to prevent context overflow errors. The key solutions are:

  1. Set model-specific context limits (95,000 tokens) and output limits (8,192 tokens)
  2. Enable auto-compaction with appropriate reserved token count
  3. Combine both methods for maximum flexibility

After applying these settings, my long coding sessions with GLM5 no longer crash with context errors. OpenCode automatically manages the context window, trimming older messages when needed while preserving recent context.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments