梦兽编程
AI_SUITE

Context Compression: Compressing Context to Keep Conversations Lean

A detailed explanation of ADK Go's Context Compression mechanism — how to compress historical messages, keep context lean, and control token consumption.

Another problem with long conversations: the history becomes too long, takes up tokens, and may exceed the context window. Compression compresses historical messages to keep them lean.

Compression vs Caching

MechanismPrincipleApplicable Scenarios
CachingCaches unchanged partsSystem prompt, knowledge base
CompressionCompresses historical messagesMulti-round conversation history

Both can be used together for better results.


How It Works

Original history:                  After compression:
[User]   Hello, I want to book a ticket    [Summary] User wants to book a ticket
[Agent]  OK, where to?                     [User] Where to?
[User]   Shanghai                          [Agent] OK, to Shanghai
[Agent]  When?                            ...
[User]   Tomorrow
[Agent]  OK, ticket for tomorrow...

Compression is not deletion; it distills lengthy conversations into short key information.


Usage

Automatic Compression

Configure a compression policy and let the framework compress automatically:

session, _ := sessions.NewSession(ctx,
    sessions.WithUserID("user-123"),
    sessions.WithAgentID("my-agent"),
    sessions.WithCompression(sessions.CompressionConfig{
        Enabled:     true,
        Threshold:   50,   // Start compressing after 50 messages
        Ratio:       0.5,  // Compress to half of the original
    }),
)

Manual Compression

session.Compress(ctx)

Compression Strategies

StrategyDescription
SummaryGenerate summary, retain key information
TruncateTruncate, keep only the most recent N messages
HybridSummarize first, then truncate

Custom Compressor

type myCompressor struct{}

func (c myCompressor) Compress(ctx context.Context, messages []string) string {
    // Custom compression logic
    // For example: only keep messages containing key information
    // Key information: numbers, time, names, intent
}

session, _ := sessions.NewSession(ctx,
    sessions.WithCompressor(myCompressor{}),
)

FAQ

Q: Does compression affect agent performance? A: Compression inevitably loses some information. If compression quality is poor, agent performance will decline. It is recommended to monitor the effect after compression.

Q: How to choose a compression strategy? A: Summary is suitable for scenarios with rich conversation content; Truncate is suitable for simple conversations; Hybrid is a balanced choice.

Q: Can compression be disabled? A: Yes, set Enabled: false. But long conversations will eventually exceed the context window, so you need to handle it yourself.


Next Steps

Finally, look at Rewind — rolling the agent back to a historical state and re-executing, suitable for when the agent goes down the wrong path and needs to start over.

Context Caching | Rewind Sessions →


Want to learn more Go ADK hands-on? Follow the 「全栈之巅-梦兽编程」 (Full Stack Summit - Dream Beast Programming) WeChat official account for weekly Go / AI programming tips.

Frequently Asked Questions

What is Context Compression in ADK Go?

Context Compression reduces the length of the conversation context by summarizing or truncating historical messages while preserving essential information.

When does Context Compression trigger?

It typically triggers when the context length or token count exceeds a configured threshold, such as a maximum number of tokens or turns.

What is the difference between Context Compression and Context Caching?

Caching stores stable context to avoid resending it; compression reduces the size of the conversation history by summarizing or dropping less important messages.

Does compression affect answer quality?

If configured properly, compression retains key facts and intent, so quality impact is minimal. Aggressive compression may remove nuances, so tuning the strategy is important.

Can I customize the compression strategy?

Yes, ADK Go usually lets you choose between strategies such as summarization, truncation, or combining both, depending on your use case.