Advanced Compression
This example shows how to set a context token based limit for tool call compression.
1"""2Advanced Compression3=============================45This example shows how to set a context token based limit for tool call compression.6"""78from kern.agent import Agent9from kern.ndx.rockspression.manager import CompressionManager10from kern.db.sqlite import SqliteDb11from kern.models.openai import OpenAIResponses12from kern.tools.websearch import WebSearchTools1314compression_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 statements24 - Funding rounds and valuations25 26 MUST REMOVE:27 - Company history and background information28 - 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 language32 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 market41 - TechCo - Feb 10, 2024: Acquired DataStart for $150M, gaining 500 enterprise customers42"""4344compression_manager = CompressionManager(45 model=OpenAIResponses(id="gpt-5-mini"),46 compress_token_limit=5000,47 compress_tool_call_instructions=compression_prompt,48)4950# ---------------------------------------------------------------------------51# Create Agent52# ---------------------------------------------------------------------------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 context61 num_history_runs=3,62 session_id="token_based_tool_call_compression",63)6465# ---------------------------------------------------------------------------66# Run Agent67# ---------------------------------------------------------------------------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, pricing75 2. Anthropic - new features, enterprise deals, funding76 3. Google DeepMind - research breakthroughs, product releases77 4. Meta AI - open source releases, research papers78 79 For each, find specific actions with dates and numbers.""",80 stream=True,81 )Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python advanced_compression.py