Environment and setup
Prerequisites (already prepared in the environment):- Python 3.11
- A virtual environment activated
- The OpenAI Python SDK installed (https://github.com/openai/openai-python)
- Working directory:
/root/code
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 atools 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 andtools 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:
- The model receives the system prompt and user question.
- Based on the
toolsmetadata, the model may decide to callcheck_calendar. - Your orchestration loop detects the tool call, executes the
check_calendarhandler, and appends the handler output as a tool message. - The model consumes the tool output and returns a final assistant response that you print.
Notes and clarifications
- The
toolslist 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
parametersschema, 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.
Quick reference links
- OpenAI Python SDK: https://github.com/openai/openai-python
- OpenAI API docs: https://platform.openai.com/docs
- Best practices for building tool-enabled agents: see official platform guides and SDK examples