Advanced Compression

This example shows how to set a context token based limit for tool call compression.

1"""
2Advanced Compression
3=============================
4
5This example shows how to set a context token based limit for tool call compression.
6"""
7
8from kern.agent import Agent
9from kern.ndx.rockspression.manager import CompressionManager
10from kern.db.sqlite import SqliteDb
11from kern.models.openai import OpenAIResponses
12from kern.tools.websearch import WebSearchTools
13
14compression_prompt = """
15 You are a compression expert. Your goal is to compress web search results for a competitive intelligence analyst.
16
17 YOUR GOAL: Extract only actionable competitive insights while being extremely concise.
18
19 MUST PRESERVE:
20 - Competitor names and specific actions (product launches, partnerships, acquisitions, pricing changes)
21 - Exact numbers (revenue, market share, growth rates, pricing, headcount)
22 - Precise dates (announcement dates, launch dates, deal dates)
23 - Direct quotes from executives or official statements
24 - Funding rounds and valuations
25
26 MUST REMOVE:
27 - Company history and background information
28 - General industry trends (unless competitor-specific)
29 - Analyst opinions and speculation (keep only facts)
30 - Detailed product descriptions (keep only key differentiators and pricing)
31 - Marketing fluff and promotional language
32
33 OUTPUT FORMAT:
34 Return a bullet-point list where each line follows this format:
35 "[Company Name] - [Date]: [Action/Event] ([Key Numbers/Details])"
36
37 Keep it under 200 words total. Be ruthlessly concise. Facts only.
38
39 Example:
40 - Acme Corp - Mar 15, 2024: Launched AcmeGPT at $99/user/month, targeting enterprise market
41 - TechCo - Feb 10, 2024: Acquired DataStart for $150M, gaining 500 enterprise customers
42"""
43
44compression_manager = CompressionManager(
45 model=OpenAIResponses(id="gpt-5-mini"),
46 compress_token_limit=5000,
47 compress_tool_call_instructions=compression_prompt,
48)
49
50# ---------------------------------------------------------------------------
51# Create Agent
52# ---------------------------------------------------------------------------
53agent = Agent(
54 model=OpenAIResponses(id="gpt-5-mini"),
55 tools=[WebSearchTools()],
56 description="Specialized in tracking competitor activities",
57 instructions="Use the search tools and always use the latest information and data.",
58 db=SqliteDb(db_file="tmp/token_based_tool_call_compression.db"),
59 compression_manager=compression_manager,
60 add_history_to_context=True, # Add history to context
61 num_history_runs=3,
62 session_id="token_based_tool_call_compression",
63)
64
65# ---------------------------------------------------------------------------
66# Run Agent
67# ---------------------------------------------------------------------------
68if __name__ == "__main__":
69 agent.print_response(
70 """
71 Use the search tools and always use the latest information and data.
72 Research recent activities (last 3 months) for these AI companies:
73
74 1. OpenAI - product launches, partnerships, pricing
75 2. Anthropic - new features, enterprise deals, funding
76 3. Google DeepMind - research breakthroughs, product releases
77 4. Meta AI - open source releases, research papers
78
79 For each, find specific actions with dates and numbers.""",
80 stream=True,
81 )

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/14_advanced
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python advanced_compression.py