Agent State

State is a centralized storage all tools can read from and write to. It's a structured way to share data between tools and the Agent, maintain conversation history, and store intermediate results.

What's Agent State?

When you call an Agent, the only information it has is what's in the prompt and the data it was trained on. If an Agent has tools and is part of a conversational system, it's useful to give it additional information, such as its past conversations, the tools it has called, and their results. You'll usually also want to allow the Agent to change this data as it goes along or share it between tools. For example, a tool that searches the web for information may need to store the search results in the state so that other tools can use it. Such external data that the Agent can read and modify is called the Agent's state.

Conversation History

State automatically stores the conversation history in the messages field. This field uses list[ChatMessage] type. By default, it appends new messages to the list so that they're included in the conversation history.

The stored messages include:

  • User messages
  • System messages
  • LLM responses (including tool calls)
  • Tool results

Defining What Data is Stored in State

You can define:

  • What data is stored in the state. You can choose which tool inputs and outputs are stored in the state.
  • The type of the data. State supports standard Python types:
    • Basic types: str,int, float, bool
    • Lists: list, list[str], list[int], list[Document]
    • Union types: Union[str, int], Optional[str]
    • Custom classes and data classes
  • How the data is merged when it's updated.