Reference · How-to · ~15 min

How to build a simple chatbot

Minimal chatbot: send user text to an LLM API, show the reply. No RAG yet — that's the next step.

Minimal chatbot: send user text to an LLM API, show the reply. No RAG yet — that's the next step.

Steps

1. **Get an API key** from a provider (OpenAI, Anthropic, etc.) — store in env vars, never in frontend code

2. **Create a backend route** so the key stays server-side

3. **Send messages** as `{ role: "user" | "assistant" | "system", content: "..." }`

4. **Optional system prompt** for tone and rules

5. **Show streaming** later for snappier UX

Minimal Python example

import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def chat(user_message: str, history: list) -> str:
    messages = [
        {"role": "system", "content": "You are a helpful, concise assistant."},
        *history,
        {"role": "user", "content": user_message},
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
    )
    return response.choices[0].message.content

Level up next

  • Add **RAG** for your docs → `build-mini-rag`
  • Add **tools** for actions → `tools-in-code`
  • Add **guardrails** → `prompt-injection` lesson
  • **Try the lessons:** `llm-api-hello` (Lane D) · `first-conversation` (Lane B)