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
| Mechanism | Principle | Applicable Scenarios |
|---|---|---|
| Caching | Caches unchanged parts | System prompt, knowledge base |
| Compression | Compresses historical messages | Multi-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
| Strategy | Description |
|---|---|
Summary | Generate summary, retain key information |
Truncate | Truncate, keep only the most recent N messages |
Hybrid | Summarize 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.
