Build Your Own AI Coding Agent

Lesson 1 of 4

LLMs

An LLM (Large Language Model) is a model trained to predict the next token in a sequence of text, given enough training data and scale, that simple objective turns out to produce something that can hold a conversation, write code, and reason through problems. This course builds a real AI coding agent, a program that uses an LLM plus tools to actually take action, starting with the basics: sending a prompt and getting a response back.

The chat message format

Every modern chat LLM API is built around a list of messages, each with a role:

  • system: instructions that set the model's behavior for the whole conversation, not something the user said, more like configuration.
  • user: what the human (or, later, your code) is asking.
  • assistant: what the model said back, you'll see this role again when building the agent's conversation history.
Pythonshares state with other Python blocks

Sending a prompt with the OpenAI SDK, via OpenRouter

OpenRouter is a service that exposes dozens of different models (from OpenAI, Anthropic, Meta, and others) behind one single, OpenAI-API-compatible endpoint, so you can use the familiar openai Python package by just pointing it at a different base_url:

from openai import OpenAI

client = OpenAI(
    base_url='https://openrouter.ai/api/v1',
    api_key='YOUR_OPENROUTER_API_KEY',
)

response = client.chat.completions.create(
    model='openai/gpt-4o-mini',
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': 'What is a large language model?'},
    ],
)

print(response.choices[0].message.content)

This needs a real OpenRouter API key and network access, so it's read-only here, get a free key at openrouter.ai to run this yourself locally.

response.choices[0].message is where the model's reply lives, .content is the actual text. choices is a list because you can ask a model for multiple candidate responses at once, for a single reply, you'll almost always just use choices[0].

TIP

Model names on OpenRouter are prefixed with the provider, openai/gpt-4o-mini, anthropic/claude-3.5-sonnet, meta-llama/llama-3.1-8b-instruct, all callable through the exact same client.chat.completions.create(...) code, only the model string changes.

📝 LLMs Quiz

Passing score: 70%
  1. 1.What does the "system" role in a messages list represent?

  2. 2.OpenRouter lets you call many different providers' models through one OpenAI-API-compatible client, just by changing the model string and base_url.

  3. 3.A model's reply text is found at response.choices[0].message.____