Skip to content

State and run store

The state is the complete record of a run: the system prompt, the conversation, what is left of the budget, and metadata. It is plain data, so a run can be saved and resumed later, inspected after it ends or moved to another machine, with no change to the loop.

Run state

@dataclass
class RunState:
    system: str | list[TextBlock]
    messages: list[Message]
    budget: BudgetState  # what is left of the budget, as of the last step
    meta: RunMeta  # e.g. {"model": "claude-haiku-5-5"}
    id: str  # generated
    parent_id: str | None = None  # the run that started this one, for sub-agents
    context_tokens: int = 0  # size of the conversation as of the last response
    data: dict[str, Any] = {}  # owned by tools and policies, saved with the run

data belongs to tools and policies: a to-do list, a permission mode or facts collected during the run. Because it is part of the state, it is saved and resumed with the conversation.

Initial state

An agent does not start from a RunState but from an InitialState: the system prompt and, optionally, messages to start with, such as examples of the expected answers. The agent turns it into a fresh RunState when it is created, and copies the messages, so one initial state can serve many agents.

@dataclass
class InitialState:
    system: str | list[TextBlock] = ""
    messages: list[Message] = []

Keeping data in the state

A tool that remembers facts in state.data:

class Remember(Tool):
    """Keeps facts in the run state, so they are saved and resumed with the conversation."""

    definition = {
        "name": "remember",
        "description": "Remember a fact for later.",
        "input_schema": schema(fact="The fact to remember."),
    }

    async def execute(self, input, ctx=None):
        facts = ctx.state.data.setdefault("facts", [])
        facts.append(input["fact"])
        return text_output(text=f"remembered ({len(facts)} facts)")

After one run, the state holds the conversation, the facts and what is left of the budget:

agent = ComposedAgent(
    model=model,
    harness=harness,
    initial_state=InitialState(system="You are a meeting assistant."),
)
await agent.run("Please remember that the meeting is on Friday.")

state = agent.state
print("id:", state.id)
print("messages:", len(state.messages))
print("data:", state.data)
print("budget left:", state.budget["steps"], "steps")

Output:

id: a291d349
messages: 4
data: {'facts': ['the meeting is on Friday']}
budget left: 3 steps

Run store

A run store saves the state after every step of the loop, so a run can be inspected while it is going, examined after it ends, and resumed.

class RunStore(ABC):
    async def save(self, state: RunState) -> None: ...
    async def load(self, id: str) -> RunState | None: ...

A store that keeps every run as JSON in memory. A store backed by files or a database follows the same pattern:

class MemoryRunStore(RunStore):
    """Keeps every run as JSON in a dict. A file or a database works the same way."""

    def __init__(self):
        self.runs: dict[str, str] = {}

    async def save(self, state):
        self.runs[state.id] = json.dumps(asdict(state))

    async def load(self, id):
        saved = self.runs.get(id)
        return RunState(**json.loads(saved)) if saved else None

Resuming a run

An agent created with a saved state continues that conversation instead of starting a new one. Its initial state is then ignored:

saved = await store.load(state.id)  # for example in another process, the next day
resumed = ComposedAgent(model=model, harness=harness, state=saved)
outcome = await resumed.run("When is the meeting?")
print(message_text(outcome.final))
print("messages:", len(resumed.state.messages), "facts:", resumed.state.data["facts"])

Output:

The meeting is on Friday.
messages: 6 facts: ['the meeting is on Friday']

The resumed agent sees the whole earlier conversation and the facts kept by the tool.

Ready blocks

The library defines the RunStore interface and leaves storage to the application. The demo agent in the repository's examples/ directory has JsonRunStore, which writes one JSON file per run.