Lesson 59.2 · 2 of 3 · 4 min read · Reviewed 24 Nov 2026
Trimming tools and history
By the end, you can
- Trim tool definitions and conversation history.
- Use prompt caching for the parts that stay the same.
Trim tools
- Give each agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. only the tools it needs (34.4).
- Keep descriptions specific but not bloated.
- Remove tools that are never called. Your logs will show which.
Trim history
- Drop small talk and repeated confirmations.
- Clear old tool results (58.3).
- Summarise older turns once the conversation is long (chapter 60).
- Start fresh for unrelated tasks.
Cache what’s stable
Prompt cachingPrompt caching: Reusing the processed form of a repeated prompt prefix across requests, which cuts cost and latency. (70.1) lets the API reuse a prefix it has seen recently. Put stable parts first: system prompt, tool definitionsTool definition: The name, description and input schema that tell the model what a tool does and how to call it., long reference documents. Put changing parts last. Cached reads cost much less than fresh input and are faster.
Caching saves money and time, but it doesn’t save attentionAttention: The mechanism that lets each token weigh every other token in the context when building its representation.: a cached 50,000-token prompt is still 50,000 tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. for the model to read. So trim first, then cache.
Key points
- Load only the tools the current task needs, and keep their descriptions tight.
- Trim history: drop small talk, clear old tool results and summarise older turns.
- Cache the stable prefix (system prompt and tools) so repeating it costs much less.
Trim tools and history, check the cache prefix
Load only the tools a task needs, trim history, and confirm the stable prefix is identical across turns so prompt caching can reuse it.
You will have: A build_request function, token counts before and after trimming, and a prefix hash check across two turns.
Steps
- Read ALL_TOOLS (40 tools) and USAGE (which tools each task type used last month).
- Write select_tools(task_type) that keeps only the tools that task type used.
- Write build_request(turn) with the stable parts first (system, tools) and changing parts last (history, time, new message).
- Hash the stable prefix with hashlib for two different turns and check the hashes match.
- Show that moving the timestamp to the front breaks the match.
Done when
- Only 6 tools are sent for the support task.
- Prefix hashes match across turns with the good layout.
- Prefix hashes differ when the timestamp is first.
Open in the Playground No API key needed.
Starter code (Python)
import hashlib
ALL_TOOLS = ["tool_%02d: does task %d with several options and long notes" % (i, i) for i in range(40)]
USAGE = {"support": [0, 3, 4, 7, 12, 19]}
SYSTEM = "You are a support agent. Follow the refund policy."
def tokens(text):
return max(1, len(text) // 4)
def select_tools(task_type):
# TODO: return only the tools used by this task type
return ALL_TOOLS
def build_request(turn, history, time_first=False):
tools = select_tools("support")
stable = [SYSTEM] + tools
changing = history[-4:] + ["time: turn %d" % turn, "user: message %d" % turn]
# TODO: if time_first, put the time line before the stable parts
return stable + changing, len(stable)
def prefix_hash(parts, n):
return hashlib.sha256("\n".join(parts[:n]).encode()).hexdigest()[:12]
history = ["turn %d text" % i for i in range(30)]
a, n = build_request(1, history)
b, _ = build_request(2, history + ["new turn"])
print("tools sent:", len(select_tools("support")))
print("prefix stable:", prefix_hash(a, n) == prefix_hash(b, n))
Show a solution
import hashlib
ALL_TOOLS = ["tool_%02d: does task %d with several options and long notes" % (i, i) for i in range(40)]
USAGE = {"support": [0, 3, 4, 7, 12, 19]}
SYSTEM = "You are a support agent. Follow the refund policy."
def tokens(text):
return max(1, len(text) // 4)
def select_tools(task_type):
return [ALL_TOOLS[i] for i in USAGE[task_type]]
def build_request(turn, history, time_first=False):
tools = select_tools("support")
stable = [SYSTEM] + tools
changing = history[-4:] + ["time: turn %d" % turn, "user: message %d" % turn]
if time_first:
return ["time: turn %d" % turn] + stable + changing[:-2] + changing[-1:], len(stable) + 1
return stable + changing, len(stable)
def prefix_hash(parts, n):
return hashlib.sha256("\n".join(parts[:n]).encode()).hexdigest()[:12]
history = ["turn %d text" % i for i in range(30)]
full = tokens("\n".join([SYSTEM] + ALL_TOOLS + history))
a, n = build_request(1, history)
b, _ = build_request(2, history + ["new turn"])
print("tools sent:", len(select_tools("support")), "of", len(ALL_TOOLS))
print("tokens untrimmed:", full, "trimmed:", tokens("\n".join(a)))
print("prefix stable:", prefix_hash(a, n) == prefix_hash(b, n))
a2, n2 = build_request(1, history, time_first=True)
b2, _ = build_request(2, history, time_first=True)
print("prefix stable with time first:", prefix_hash(a2, n2) == prefix_hash(b2, n2))