Cache Model Response

Example showing how to cache model responses to avoid redundant API calls.

1"""
2Cache Model Response
3=============================
4
5Example showing how to cache model responses to avoid redundant API calls.
6"""
7
8import time
9
10from kern.agent import Agent
11from kern.models.openai import OpenAIResponses
12
13# ---------------------------------------------------------------------------
14# Create Agent
15# ---------------------------------------------------------------------------
16agent = Agent(model=OpenAIResponses(id="gpt-4o", cache_response=True))
17
18# ---------------------------------------------------------------------------
19# Run Agent
20# ---------------------------------------------------------------------------
21if __name__ == "__main__":
22 # Run the same query twice to demonstrate caching
23 for i in range(1, 3):
24 print(f"\n{'=' * 60}")
25 print(
26 f"Run {i}: {'Cache Miss (First Request)' if i == 1 else 'Cache Hit (Cached Response)'}"
27 )
28 print(f"{'=' * 60}\n")
29
30 response = agent.run(
31 "Write me a short story about a cat that can talk and solve problems."
32 )
33 print(response.content)
34 print(f"\n Elapsed time: {response.metrics.duration:.3f}s")
35
36 # Small delay between iterations for clarity
37 if i == 1:
38 time.sleep(0.5)

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 cache_model_response.py