Skip to main content
Five components. That’s all an agent needs: a model, tools, memory, a system prompt, and an orchestration loop. In this lesson we’ll wire all five together in a single Python script and observe them working as one. This walkthrough uses the OpenAI Python SDK and demonstrates a simple tool-driven agent that checks a calendar.

Environment and setup

Prerequisites (already prepared in the environment): Create a new file named agent_components.py and follow the steps below.

Overview: the five components

Step 1 — Skeleton and client initialization

Start with the basic imports and client setup: create an OpenAI client using your API key and base URL from environment variables, select the model, and initialize conversation messages with a system message.
The system prompt above is component one. It gives the agent an identity and instructs it to use tools rather than guessing when it needs real data.

Step 2 — Define a tool (and its handler)

Add a tools list that describes what the model may call. This is the metadata the model sees. Separately, implement a handler function in Python that executes the requested action and returns the result.
  • Tool metadata (name, description, and parameters) is provided to the model.
  • Handlers perform the real work and return text results to be appended back into the conversation.
Remember: the model reads the tool definition (the tools list) and may decide to call check_calendar. Your code must provide the handler (the check_calendar function) to produce the actual result.

Step 3 — Add a user message

Append a user message that will trigger the agent to use tools as needed.

Step 4 — Orchestration loop

The orchestration loop sends messages and tools metadata to the model, inspects the model response for tool calls, executes handlers when requested, and feeds the results back to the conversation until the model returns a final answer.

Step 5 — Run the script

From the terminal in /root/code, run:
What happens:
  • The model receives the system prompt and user question.
  • Based on the tools metadata, the model may decide to call check_calendar.
  • Your orchestration loop detects the tool call, executes the check_calendar handler, and appends the handler output as a tool message.
  • The model consumes the tool output and returns a final assistant response that you print.
Expected final output (example):

Notes and clarifications

  • The tools list describes the functions the model may call. The model sees the metadata (name, description, and parameter schema) but does not execute code itself.
  • Handlers are implemented in your runtime (Python in this example). They can call APIs, access databases, or return simulated responses.
  • The orchestration loop is the glue: it passes conversation state and tool definitions to the model, inspects for tool calls, runs handlers, and appends tool output so the model can finish its response.
  • For production agents, implement robust error handling, retries, input validation against the declared parameters schema, and logging of tool calls and results.
Keep your API key and base URL secure. Never commit them to source control; prefer environment variables or secrets managers. If keys are exposed, rotate them immediately.
This example demonstrates a minimal, clear pattern for building a tool-enabled Python agent: define what the model can call, implement the handlers, and run an orchestration loop to coordinate model decisions and tool execution.

Watch Video