-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude_agent_keep_recent.py
More file actions
49 lines (35 loc) · 1.38 KB
/
claude_agent_keep_recent.py
File metadata and controls
49 lines (35 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Example: Using KeepRecentMessages with claude-agent-sdk.
This example shows how to integrate context compaction with the
Claude Agent SDK using the PreCompact hook.
Requirements:
pip install context-compactor[claude-agent]
"""
from __future__ import annotations
import asyncio
from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient
from context_compactor import ContextCompactor
from context_compactor.adapters.claude_agent import claude_agent_adapter
from context_compactor.strategies import KeepRecentMessages
from context_compactor.tokenizers.claude_agent import ClaudeAgentTokenCounter
# Create the compactor
compactor = ContextCompactor(
max_context_tokens=200_000, # Claude's context window
trigger_at_percent=0.8,
strategy=KeepRecentMessages(keep_count=20),
token_counter=ClaudeAgentTokenCounter(),
verbose=True,
)
# Create hook configuration
hook_event, hook_matchers = claude_agent_adapter(compactor)
async def main():
"""Run a conversation with Claude Agent SDK."""
options = ClaudeAgentOptions(
hooks={hook_event: hook_matchers},
)
async with ClaudeSDKClient(options=options) as client:
for i in range(25):
await client.query(f"Tell me about feature {i}")
print(f"Turn {i + 1}: Response received")
print(f"\nStats: {compactor.get_stats()}")
if __name__ == "__main__":
asyncio.run(main())