Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Agent State
Agent state is the record of everything the agent knows and has done during the current task. Without state, the agent cannot make informed decisions about what to do next.
What Is Agent State?
Agent state is the working memory of the agent for the current task.
State includes:
- The original goal
- What steps the agent has completed
- What tools were called and with what arguments
- What results were returned
- What the agent has learned so far
- How many steps remain
- Whether any errors have occurred
State is updated after every action. The controller reads the updated state before deciding on the next action.
Without state, the agent would forget what it has already done and would repeat steps, contradict itself, or lose track of the goal.
State Data Structure
# Example agent state structure in Python
agent_state = {
"goal": "Research top 3 web scraping libraries",
"step_count": 2,
"max_steps": 10,
"status": "in_progress", # in_progress | complete | escalated | failed
"history": [
{
"step": 1,
"action": "web_search",
"arguments": {"query": "top Python web scraping libraries"},
"observation": "Found: BeautifulSoup, Scrapy, Playwright, Selenium..."
},
{
"step": 2,
"action": "web_search",
"arguments": {"query": "BeautifulSoup vs Scrapy comparison 2024"},
"observation": "BeautifulSoup is best for simple pages, Scrapy for large crawls..."
}
],
"errors": [],
"result": None # Will be filled when goal is complete
}
print("Current step:", agent_state["step_count"])
print("Status:", agent_state["status"])