Foundations: LLM Principles and APIs Β· Milestone Companion: Your First Agent Is a Loop, Not a Framework
What this milestone asks
Build, in a single file of under 100 lines, a conversational agent that can call tools β the classic pairing is a weather lookup plus an arithmetic evaluator β using nothing but an LLM API. No LangChain, no LlamaIndex, no agent SDK. Just you, the messages array, a hand-written tool schema, and a while loop.
Concretely, your program must: accept a user question in a chat loop; send the conversation to the model together with tool definitions; detect when the model replies with a tool call instead of plain text; execute the corresponding function; append the tool result to the messages; and repeat until the model produces a final answer. The arithmetic tool must compute in code β if the model is doing math in its head, you have built a demo, not an agent.
The 100-line budget is the point. It is short enough that you must understand every line, and long enough to contain the entire skeleton that every framework later wraps in abstraction.
Why this milestone
Everything in the next four stages is this loop with elaborations. ReAct is this loop with a reasoning trace. LangGraph is this loop with explicit state. MCP is a standard way to serve the tools this loop calls. If you have never assembled the loop yourself, every framework will feel like magic β and magic is impossible to debug.
The milestone also forces the three Stage-1 insights into your fingers rather than your notes. You will feel the context window when you decide what to append after each tool call, and what to trim. You will feel tokens-as-cost when you log usage per turn and watch the system prompt get billed on every single request. And you will understand structured output viscerally the first time the model emits a malformed tool call and your dispatcher crashes β which is exactly why JSON mode and schema-constrained output are engineering cornerstones, not conveniences.
How to use the resources
Sequence matters more than coverage. A workable order:
- Intuition first (a weekend). Watch 3Blue1Brown's neural-network series, especially the attention episodes, and Karpathy's Intro to LLMs. You are not learning to build a Transformer; you are building a correct mental picture of the "attention-weighted information filter" so that later terminology has somewhere to land.
- Mechanism (one to two weeks, selective). If your calculus and linear algebra are fresh, take the early lectures of MIT 6.S191 β the 2026 edition open-sources lectures, slides, and labs weekly from March 30. Alternatively, sample the Transformer lectures of Stanford CS224N (Winter 2026, Diyi Yang and Yejin Choi). Do not try to complete these courses now; you are mining them for the four or five lectures that explain attention, embeddings, and decoding.
- Depth, optional and parallel. Stanford CS336 (Language Modeling from Scratch, Percy Liang and Tatsunori Hashimoto, Spring 2026) is where you go to understand tokens at the level of writing a tokenizer. Treat it as a parallel track, not a gate.
- API craft (the actual build week). Work through the OpenAI Cookbook's function-calling and structured-output examples, then read the tool-use sections of Anthropic's prompt-engineering documentation β widely regarded as the most systematic treatment of tool descriptions. Write your two tool schemas by hand before you write any loop code. Mu Li's paper deep-readings are your fallback whenever a concept (temperature, sampling, context) refuses to become concrete.
Then build. Harvard CS50 AI is there if you discover pre-deep-learning gaps (search, knowledge representation) bothering you; most builders will not need it for this milestone.
Common failure modes
- Starting inside a framework. If your first agent imports LangChain, you have skipped the stage. Delete it and restart.
- Fake tools. The model "answers" the weather question from training data because your tool description never told it when to call. Tool descriptions are behavioral contracts: write when-to-use, not just what-it-does.
- No stop condition. A
while Truewith no maximum-iteration guard is an infinite API bill waiting for one confused model response. - Mental arithmetic. Accepting the model's computed answer instead of routing it through your function. The entire point of tools is that the model proposes and code disposes.
- Token blindness. Never logging
usage. Estimate per-session cost once, by hand, or the habit never forms. - Prompt archaeology. Patching unexpected behavior with more prompt text instead of inspecting the raw messages array. At 100 lines you can print everything; do it.
Acceptance checklist
- One file, under 100 lines, importing only an LLM SDK (or raw HTTP) and the standard library β no agent framework.
- Two working tools (e.g., weather + arithmetic); arithmetic is computed by your code, never by the model.
- Tool schemas are hand-written JSON with name, description, and typed parameters.
- The loop has an explicit stop condition and a maximum-iteration guard.
- A tool error (unknown city, malformed expression) returns a readable message to the model instead of crashing.
- Token usage is logged per call, and you have computed the cost of a 10-turn session by hand.
- You can explain β out loud, without notes β what
messages,temperature, andtoolseach do.
Going further
Dissect a tokenizer on your own prompts and watch where tokens actually go. Re-run your agent at temperature 0 and 1 on identical inputs and log the difference in tool-call reliability. Then open the CS336 assignments and write a tokenizer yourself β after this milestone, you have earned it. When the loop starts to feel cramped (you want memory, retrieval, more tools), that discomfort is the correct entry point to Stage 2.