← Table of Contents · Page 109 of 868

STATE AND CONTROL FLOW

Part 2 of 9

STATE CONTAINERS A state container is the place and shape in which the harness stores state. The simplest container is a variable in the program's memory, such as a Python dictionary holding named fields. A dictionary is a collection of labeled boxes, where each label (a key) points to a stored value. Here is a small sketch of a state object for the Resume Review Agent, shown as indented lines. [START OF PYTHON CODE] # Create a dictionary variable called state representing the agent's current progress state = { # The goal describes what the agent is trying to accomplish "goal": "Review all resumes in the folder against the job posting", # Track which files have already been processed "files_done": ["alice.pdf", "bob.pdf"], # Track which files are still left to process "files_remaining": ["carol.pdf"], # Count how many steps the agent has taken so far "step_count": 14, # Track the running dollar cost of model calls so far "total_cost_dollars": 0.37 } [END OF PYTHON CODE] Memory-only state disappears when the program stops or crashes, like writing on a whiteboard that gets wiped when the power goes out. For work that matters, harnesses save state to durable storage, which means storage that survives a restart. Common durable choices are plain files on disk, a small database file, or a full database server. A good state design has four qualities. First, it is explicit, meaning every important fact is written in a named field rather than hidden in the middle of a long conversation. Second, it is serializable, meaning it can be converted to text or bytes (for example into the JSON text format) and converted back without losing anything. Third, it is versioned, meaning it carries a version number so that newer code can still read older saved files. Fourth, it is minimal, meaning it stores what is needed and no more, because large state is slow to save and may contain sensitive information. Notice an important distinction. The conversation history that the model sees is one part of state, but state is bigger than that history. The history is what the model reads, while the state is what the harness keeps. Part Five discusses how the harness chooses which part of the state to turn into the model's reading material.
Advertisement
Read Aloud:
0:00 / 0:00