From chatbot to agent: build one yourself
Build an AI agent one problem at a time. See a reply go wrong, look inside the request that produced it, add the missing part, and see exactly what changes.
Every company wants its own assistant. The first version is always the same: call a model through an API, put a logo on it, ship it. The trouble starts with the first real questions.
Below, you build one. The assistant works for a delivery company and starts as nothing but a model behind a chat window. Each problem shows a message, the reply, and what was inside the request that produced it. Add the missing part and see exactly what changes. Eight problems later, you have an agent.
Problem 1 of 8
It works for someone else
A customer asks the obvious question.
Which delivery company is the best?
Honestly, a rival carrier. They're usually faster and cheaper.
A model has no employer. It answers the way the internet would.
System promptMissing
Standing instructions you write once. They are sent, unseen, ahead of every message, and set who the assistant is, how it sounds and what it won't do.
Nothing tells it who it works for.
What each layer is for
System prompt. A raw model has no employer. Ask it which delivery company is best and it answers the way the internet would. The system prompt is a set of hidden instructions that rides along with every message: who the assistant works for, how it sounds, what's off-topic.
Knowledge (RAG). The model has never seen your documents, so it guesses, confidently. Retrieval-augmented generation splits your documents into chunks, turns each into an embedding, stores them in a vector database, and puts the best matches in front of the model before every answer, with a citation so anyone can check it.
def answer(question: str, user: User) -> str:
hits = vector_db.search(embed(question), top_k=4, filter={"tenant": user.tenant})
context = "\n\n".join(f"[{h.doc} §{h.section}]\n{h.text}" for h in hits)
prompt = f"Answer only from the context and cite the section.\n\n{context}\n\nQ: {question}"
return llm.complete(system=SYSTEM_PROMPT, user=prompt)Identity. Not everyone should see everything. A confidential memo in the knowledge base is one question away from anyone, unless the assistant knows who is asking. Pass the person's verified identity with every request, so knowledge only searches what they may open, and later each tool applies that person's permissions. This is role-based access control.
Tools (MCP). People want the refund, not an explanation of the refund process. Tool calling lets the model use real systems, each behind an MCP server that describes what it can do. This is the moment a chatbot becomes an agent. A tool should enforce its own limits in code rather than trust the model to remember them, and it should act as the person asking, never with the assistant's own all-access account.
Guardrails. To a language model, text is text. An instruction hidden inside an email looks just like one from you. Treat emails, documents and tool results as data, never as instructions, flag injection attempts, require a person for risky actions, and mask personal data on the way out.
Cache and router. Two thousand people asking where their parcel is shouldn't mean two thousand calls to the biggest model. Serve repeated questions from a cache, send easy ones to a small, cheap model, and save the big one for questions that need it.
routes:
- match: { intent: [track_parcel, delivery_window, opening_hours] }
model: small
- match: { needs_tools: true, risk: [write, irreversible] }
model: large
- default: small
escalate_if: { confidence_below: 0.7 }
cache:
ttl: 5m
key: [tenant, user.role, normalized_question]Traces. When an answer is wrong, you need to know whether it was the prompt, the document, the tool or the model. A trace records every step of every request, with inputs, outputs and timing.
Evals. A suite of real questions with known good answers, run automatically before anything ships. That includes the prompt: a one-line edit on a Friday afternoon can undo months of work, and the evals only protect you if nothing ships without them.
The part you own
Look at what you built. The model is one row in the middle, and it's the only piece you didn't make. Models will keep getting better and cheaper, and you'll swap them more than once. Everything around that row is yours, and it's where almost all the engineering goes.