4. Execution and State Management

Watching the agent think. Observing the thought/action/observation loop in real-time and managing its working memory.

We have our tools and our system prompt. The final step is to write the while loop in Python that actually runs the agent. This loop is the “Orchestrator.”

The Orchestrator Loop

The logic is deceptively simple:

  1. Send the entire conversation history (State) to the LLM.
  2. Wait for the LLM to reply.
  3. If the reply contains the word “Answer”, we are done. Break the loop and print the final report.
  4. If the reply contains an “Action”, extract the JSON, run the Python tool, append the result as an “Observation” to the conversation history, and go back to step 1.

Visualizing the Logs

When you run this script in your terminal, it is incredibly satisfying to watch the agent “think” in real-time. A healthy execution log should look like this:

Thought: The user wants to know about solid-state batteries. I need to search the web for recent articles.
Action: {“tool”: “search_web”, “args”: {“query”: “solid state battery breakthroughs 2026”}} PAUSE

Observation: [Google Search Results: 1. “Toyota announces new solid state…”, 2. “QuantumScape breakthrough…”]

Thought: I have the search results. I should read the first article from Toyota to get specific details for the report.
Action: {“tool”: “read_url”, “args”: {“url”: “https://toyota-news.example.com”}} PAUSE

Observation: [Full text of the article…]

Thought: I have enough information. I will now format the markdown report and save it.
Action: {“tool”: “save_file”, “args”: {“filename”: “battery_report.md”, “content”: “# Solid State Batteriesn…”}} PAUSE

Observation: File successfully saved to disk.

Thought: The file is saved. I have fulfilled the user’s request.
Answer: The report has been generated and saved as battery_report.md.

Managing State (The Context Window)

Notice that every time we loop, the conversation gets longer. If the read_url tool returns 4,000 words of text, that is permanently added to the agent’s memory. If the agent reads 5 articles, the conversation history will explode, and the model will crash (OOM).

The Solution: You must implement logic in your Python loop that monitors the token count. If the conversation history gets too long, you must programmatically truncate the older Observations before sending the prompt back to the LLM, keeping only the most recent and relevant data in its “working memory.”