Search⌘ K
AI Features

Part 1: Prefix, Hybrid Retrieval, and Offloading

Build the project’s prefix, retrieval, and execution layers by assembling tested components into shared files. You’ll verify cache stability, history and retrieval budgets, quote grounding, edge ordering, and sandbox limits while learning why isolation matters for code generated from untrusted context.

We'll cover the following...

In the last lesson, we wrote the project’s specification: six files, a 1,063-token fixed prefix, a 16,000-token working budget with a cap for each part, the cache breakpoint, and the trust boundaries. None of the project files exists yet.

In this lesson, we’ll write three of them: context_prefix.py, retrieval.py, and runner.py. Each one assembles pieces we built and tested in earlier chapters. The new work is the assembly. Moving tested code into a shared file can break it in ways the earlier tests never checked, so after each file we’ll rerun the check that proved the original, now against the assembled file and the budget from the specification.

Write the prefix file

Every request depends on the exact bytes before the cache breakpoint, so context_prefix.py comes first. It holds three rules from Chapter 3 and one from Chapter 5:

  • Fixed prefix: We always send the same six tools and the same system prompt, in the same order.

  • Masking with tool_choice: We pick the tool mode from the state machine, so the tools block itself never changes.

  • Dynamic content after the breakpoint: We put retrieved notes, memory, history, and the question in the messages.

  • Compaction: We archive old turns and summarize them once the history passes its threshold, applying the Write and Compress moves from Chapter 1.

Here’s the file.

context_prefix.py · Python 3.14.0
from altitude_prompt import ALTITUDE_PROMPT
from compact_session import compact_session
from long_session import render
from project_tools import PROJECT_TOOLS
from render_request import render_prefix
from state_machine import next_state
from states import REQUIRED, SPECIFIED
TOOL_NAMES = [tool["function"]["name"] for tool in PROJECT_TOOLS]
def tool_choice(turn): # mask with tool_choice, so the tools block itself never changes
state = next_state(turn)
if state == SPECIFIED:
if turn["next_tool"] not in TOOL_NAMES:
raise ValueError(f"unknown tool: {turn['next_tool']!r}")
return {"type": "function", "function": {"name": turn["next_tool"]}}
return "required" if state == REQUIRED else "auto"
def build_request(turn, context, history, summarize):
history, _ = compact_session(history, summarize) # archive first, then summarize, past the threshold
dynamic = f"{context}\n\nEarlier turns:\n{render(history)}\n\nQuestion: {turn['question']}"
return {"tools": PROJECT_TOOLS, "tool_choice": tool_choice(turn),
"messages": [{"role": "system", "content": ALTITUDE_PROMPT}, {"role": "user", "content": dynamic}]}
def prefix_of(request): # the cached part, up to the breakpoint: tools, then the system prompt
return render_prefix(request["tools"], request["messages"][0]["content"])

In the code above:

  • Line 9: We list the tool names from the six project tools.

  • Lines 12–18: We define tool_choice(). It asks next_state() from Lesson 3.4 for the mode. A known next tool becomes a forced choice, after a check that the tool exists. Otherwise the mode is required or auto.

  • Lines 21–25: We define build_request(). It compacts the history with compact_session() from Lesson 5.2, then puts the context, the history, and the question in the user message. The tools and the system prompt never change.

  • Lines 28–29: We define prefix_of(), which renders the part of the request that gets cached.

Check that the prefix holds

The cache ...