会话修剪
Session pruning: tool-result trimming to reduce context bloat
Session pruning trims ''old tool results'' from the in-memory context right before each LLM call. It does ''not'' rewrite the on-disk session history (''*.jsonl'').
When it runs
- ''mode: "cache-ttl"''但启用在、会话的最后一个人間消息但''ttl''比旧場合。
- Only affects the messages sent to the model for that request.
- Only active for Anthropic API calls (and OpenRouter Anthropic models).
- For best results, match ''ttl'' to your model ''cacheControlTtl''.
- After a prune, the TTL window resets so subsequent requests keep cache until ''ttl'' expires again.
Recommended TTL Value
修剪是工具结果在仅適用被:
- ''tool_result''消息
- ''tool_use'' messages are ''not pruned'' (needed for the LLM to know which tool it called)
修剪策略
Currently supported strategies:
- Context size reduction: old tool results are removed, making context smaller
- Cost reduction: API calls are made with smaller context, reducing costs
ReferenceConceptsSessionPruningPage step 03: P4
设置例
有效使用会话修剪的最佳实践:
- Match TTL to models' ''cacheControlTtl''
{
"sessionPruning": {
"mode": "cache-ttl",
TTL的推荐值
如果修剪未按预期工作,请进行故障排除:
- Claude 3.5 Sonnet:''cacheControlTtl''
- Confirm you're using Anthropic API (doesn't work with other providers)
- Confirm the session's last human message is older than TTL
修剪的影響
修剪的限制
修剪在是下一个限制有:
- Anthropic API(以及OpenRouter Anthropic模型)在仅機能执行
修剪的监控
修剪的动作监控执行在是、日志确认执行:
- Only works with Anthropic API (and OpenRouter Anthropic models)
- Only tool results are pruned (other message types are not affected)
- 修剪後的上下文大小但日志在显示被
修剪的禁用
要监控修剪行为,请检查日志:
```json
{
"sessionPruning": {
最佳实践
有效使用会话修剪的最佳实践:
- Match TTL to models' ''cacheControlTtl''
故障排除
如果修剪未按预期工作,请进行故障排除:
- Confirm TTL value matches models' ''cacheControlTtl''
- Confirm you're using Anthropic API (doesn't work with other providers)
- Confirm the session's last human message is older than TTL
- Check logs to confirm pruning is running
ReferenceConceptsSessionPruningPage step 11: P6
ReferenceConceptsSessionPruningPage step 11: P7
总结
会话修剪是防止长会话中上下文膨胀的强大工具。使用基于TTL的修剪策略,您可以删除旧的工具结果,减少上下文大小,并改善成本和性能。
{
agent: {
contextPruning: { mode: "off" },
},
}It works with Anthropic API and coordinates with model cache control settings to provide optimal results.
{
agent: {
contextPruning: { mode: "cache-ttl", ttl: "5m" },
},
}ReferenceConceptsSessionPruningPage step 12: P3
{
agent: {
contextPruning: {
mode: "cache-ttl",
tools: { allow: ["exec", "read"], deny: ["*image*"] },
},
},
}ReferenceConceptsSessionPruningPage step 12: P4