-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy path5_cache_config_openai.py
More file actions
40 lines (34 loc) · 1.06 KB
/
Copy path5_cache_config_openai.py
File metadata and controls
40 lines (34 loc) · 1.06 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
"""
Prompt caching with OpenAI via cache_config.
OpenAI caches automatically for prompts >= 1,024 tokens (no cache_control
markers needed). `cache_config` passes through OpenAI's two optional controls:
* prompt_cache_key — routing hint that raises cache hit rates
* prompt_cache_retention — "in_memory" (default) or "24h"
export OPENAI_API_KEY="sk-..."
python 5_cache_config_openai.py
"""
from swarms import Agent
SYSTEM_PROMPT = (
"You are a senior financial analyst. Ground every claim in fundamentals, "
"state assumptions explicitly, and provide analysis only — never "
"personalized investment advice. " * 200
)
agent = Agent(
agent_name="Analyst",
system_prompt=SYSTEM_PROMPT,
model_name="gpt-5.4",
prompt_caching=True,
cache_config={
"prompt_cache_key": "analyst-v1",
"prompt_cache_retention": "24h",
},
max_loops=1,
)
print(
agent.run(
"Give me a 3-point framework for valuing a SaaS company."
)
)
print(
agent.run("Now do the same for a bank.")
) # hits the automatic cache