Dissecting AI Agents · Part 1

Inside an AI Agent

In the first episode of Dissecting AI Agents, we turn a Python chatbot into an agent. We keep the model, give it one tool, and build the loop that returns the result.

Building the Agent Loop5:37 · English captions

Trace the tool call

Inside an AI agent A user asks which files are in a folder. The model requests Bash with the command ls -a and an illustrative call ID, call_7. Python runs Bash on the Mac. The output includes pyproject.toml, release-session.jsonl and workshop.txt. Python puts that output back in the model’s context with the same call ID. On its next turn, the model names all three files. AGIpilled AI SIMPLIFIED · EPISODE 1 Inside an AI agent YOU ASK Which files are in this folder? MODEL Request a tool bash {"command": "ls -a"} Example ID call_7 PYTHON + BASH Run it on the Mac $ ls -a . .. pyproject.toml release-session.jsonl workshop.txt MODEL CONTEXT Return the result Python sends the filenames back with call_7 MODEL’S NEXT TURN The model can now name all three files. Alexander Dess agipilled.blog

The same call ID connects the filenames that came back from my Mac to the command the model requested.

Download the diagram (PNG)

Send the conversation

DessChat.py sends the user’s message and the conversation so far to the model. It keeps the reply in history for the next turn and prints the text.

DessChat.py
history.append({"role": "user", "content": question})
reply = client.responses.create(
    model=os.getenv("OPENAI_MODEL", "gpt-6.2-sol"), instructions="Answer briefly in plain text.",
    input=history, store=False, include=["reasoning.encrypted_content"],
)
history.extend(reply.output)
print(f"\nDessChat> {reply.output_text}\n")

Give the model a tool

In DessCode.py, we pass tools=[BASH] with the same request. The tool definition tells the model it can ask for a shell command using one string argument, command. Python checks the reply for that request.

DessCode.py
calls = [item for item in reply.output if item.type == "function_call"]
if not calls:
    return reply.output_text

Run the command and return the result

In the video, the model requests ls -a. Python runs it and puts the output in history. The call_id ties those filenames to the command that produced them.

DessCode.py
call = calls[0]
command = json.loads(call.arguments)["command"]
print(f"\nBash> {command}", flush=True)
output = run_bash(command)
print(output, flush=True)

# Build the next call's context. Match the result to the original request.
history.append({"type": "function_call_output",
                "call_id": call.call_id, "output": output})

The loop sends that updated history to the model. It can request another command or answer in words. This example allows up to five requests per question.

The film uses Bash on my Mac. The GitHub workshop runs the same exercise in a disposable Linux container.

What should we dissect next? Tell me on LinkedIn.