Skip to main content
You’ve seen code snippets throughout this section — the agent loop, tool definitions, and system prompts — but you haven’t executed any of it yet. This lesson gets you hands-on: you’ll install the SDK, create a minimal Python program that sends a message to an LLM, and print the response. From there we’ll incrementally extend the same pattern until you have the foundation used later in the course. What you’ll build
  • A working Python script that calls the chat API and prints the assistant reply.
  • A short pattern you can reuse: create client → build messages → send request → read response → append to history.
Prerequisites
  • Python 3.7+
  • pip available on your PATH
  • Access to the OpenAI-compatible base URL and API key (in the exercise environment these are provided by CodeKeys)
Install the OpenAI Python SDK We use the OpenAI Python SDK as a client library so you can call a simple function instead of managing raw HTTP requests.
You should see output similar to:
Create the project and file
Create the client Open agent.py and add the imports and a client object. The OpenAI client holds the connection settings (base URL and API key) and performs API calls. Recommended: read credentials from environment variables so they are not committed to source control:
Note: in the exercise environment, CodeKeys will provide both the base_url and api_key. Never commit your API keys to source control.
Store sensitive credentials (API keys, base URLs) in environment variables or a secrets manager. Do not hardcode them in source files.
Make your first chat call Build a messages list (the conversation history), call client.chat.completions.create, and print the assistant content. This example includes a system message to control tone and a user question:
Run the script:
You should see the LLM reply printed in your terminal. The important pattern to observe is the conversation is sent as messages so the model responds in the context you provide. The system message (role: "system") sets the model’s persona or behavior before any user messages arrive. Conversation memory (short back-and-forth) To continue a multi-turn conversation, append the assistant reply to messages and include your next user prompt. Each API call sends the full history, which is how the model retains context.
Best-practice checklist
  • Create the client once at the top of your file.
  • Start messages with a system prompt that sets tone/behavior.
  • Send user messages as role: "user".
  • Append assistant replies to messages after each call.
  • Avoid hardcoding secrets; use env vars or a secrets manager.
Foundation pattern (quick reference)
A neon-styled diagram titled "THE FOUNDATION PATTERN" showing six numbered steps for a chat/API workflow: 1 Create Client, 2 Build Messages, 3 Add User Message, 4 Call the API, 5 Read Response, and 6 Append to History, with a note "Lab 03 extends with Tools."
Next steps Every code snippet in this section follows this same foundation pattern: create the client, build the messages list (start with the system message), add the user message, call the API, read the response, and append it to history so the model remembers context on the next call. A subsequent lab builds on this by adding tools, wrapping the flow in an agent loop, executing tools, and handling errors. References

Watch Video

Practice Lab