Visual walkthrough — Agentic frameworks (LangChain, LlamaIndex)
The parent note told you what the agent loop is. This page builds it from nothing — starting from one stubborn fact about language models — and draws every single step. By the end you will be able to redraw the whole machine on a napkin.
We will keep coming back to that box. Watch how much we get from it.
Step 1 — The box that can only talk
WHAT. We draw the language model as a single box. On the left goes a prompt (the text we give it). On the right comes completion (the text it gives back).
WHY. Before we can give a model "tools" we must be honest about what it physically does. If we forget that it only emits text, we will invent magic that does not exist. Every later step is a workaround for this box's blindness.
PICTURE. The blue box is the model. The orange arrow in is our prompt; the green arrow out is its text reply. Notice there is no wire to the calculator or the web — it is sealed.
Step 2 — Teach the box to ask for a tool
WHAT. We agree on a fixed text shape the model will write when it wants a tool. For example:
WHY. The box cannot press a button, but it can write the sentence "I would like to press the calculator button with 14000 * 2". If we agree in advance on the exact wording, our own code — running outside the box — can watch for that wording and press the button for it. This is the whole trick: the model requests, our code acts.
PICTURE. The model outputs ordinary text (green), but part of that text follows our agreed template (highlighted). It is a message in a bottle, not an action.
Cloze check:
The Action: line is executed by
Step 3 — The parser: turning a wish into an instruction
WHAT. We write a small function, the parser, that reads the model's text and pulls out two things: the tool name and the argument.
Term by term:
- — the raw completion from Step 2.
- — a clean little packet: "call tool with input."
- — the packet that means "I'm done, here is the reply."
WHY. The model wrote free-form prose. Our tool functions need tidy arguments. The parser is the translator between messy text and clean function calls. Without it, we would be doing string surgery everywhere.
PICTURE. A funnel: messy green text goes in the top; two clean labelled boxes fall out the bottom — one orange (AgentAction), one green (AgentFinish). A gray "??" branch shows the third case.
Step 4 — The tool registry runs it and gets a result
WHAT. We keep a lookup table — the registry — mapping each tool name to a real Python function. Given an AgentAction, we look up the function and run it.
Term by term:
- — pick the real function whose name matches.
- — feed it the argument the parser extracted.
- — the value the function returns (a number, a Wikipedia snippet, an error message).
WHY. This is the only place where anything real happens — a number gets multiplied, a page gets fetched. The model is still sealed in its box; our code reached out and touched the world on its behalf.
PICTURE. The AgentAction packet arrives at a switchboard. It routes to the matching function (calculator, Wikipedia, ...), which returns an Observation (green). Note an error is also a valid observation.
Step 5 — Feed the observation back and loop
WHAT. We glue the observation onto the growing conversation and call the box again:
Term by term:
- — everything so far: the question, the model's action, and now...
- — the freshly computed result, written as text the model can read.
- — the new, longer prompt for the next turn.
WHY. Remember the box has no memory (Step 1). The only way it learns "the population is 14 million" is if we write that fact into its next prompt. The prompt is its entire world; we keep re-showing it a bigger and bigger transcript.
PICTURE. A spiral: prompt → LM → action → tool → observation → appended → back to a fatter prompt. Each loop the transcript grows by one Action/Observation pair.
Reveal:
Why must the observation be appended as text rather than stored in a variable the model reads?
Step 6 — When does it stop? (and when it shouldn't run forever)
WHAT. The loop ends the moment the parser returns an AgentFinish — i.e. the model wrote Final Answer:. We return that answer and exit.
WHY. Without a stop rule the spiral in Step 5 never ends. Two dangers, both must be handled:
- Normal stop — model is satisfied and writes
Final Answer:. - Runaway stop — the model loops forever (keeps calling tools, never finishing) or ping-pongs between two tools. We add a hard cap: max iterations . After turns we force-return whatever we have.
PICTURE. A flow with two exits: the green "Final Answer" door, and a red "iteration limit reached" trapdoor. Both leave the loop; only the green one is a happy ending.
Here is the safety cap and "turns used" counts each LM call — the inequality is what guarantees termination even for a stubborn model.
The one-picture summary
Everything above is one loop wrapped around one sealed box. Here it is on a single canvas: the blue box that can only talk, the parser funnel, the tool switchboard, the observation feedback arrow, and the two exit doors (green finish, red cap).
Recall Feynman retelling (say it out loud)
I have a machine that can only read and write text — nothing else. I want it to do maths and look things up. So I make a deal: when it wants a tool, it writes a special sentence like Action: Calculator, Input: 14000*2. I stand outside the machine reading everything it writes. The moment I spot that sentence, I run the calculator, get 28000, and I write back into the machine's notebook: Observation: 28000. Then I let it read again. It keeps requesting tools; I keep running them and pasting the results back, growing the notebook each time. This continues until the machine writes Final Answer: — then I hand that to the user and stop. And because a stubborn machine might loop forever, I also promise myself: after N rounds I quit no matter what. That whole dance — request, translate, run, paste back, repeat, stop — is the agent. The model never touched a tool; it only ever wrote wishes, and I granted them.
Connections
- Built directly on ReAct — this page is ReAct turned into code.
- The
Action:format is a hand-rolled version of tool/function-calling APIs. - A
Wikipediaor document tool often wraps RAG over a vector database. - Prefer the Hindi walk-through? Yeh note Hinglish mein padho →