Agents Demystified
The preceding chapters reduced the model to its operating core: context in, predicted tokens out. A model can emit prose, code, a reasoning trace, or a tool-call-shaped message, but it is still predicting tokens from the supplied context.
An agent is the software system wrapped around that model call. The harness supplies context, exposes tools, parses structured outputs, executes approved actions, appends observations, and decides whether to continue. The LLM is the probabilistic engine; the agent is the deterministic control plane that turns its outputs into useful work.
The Agent Loop
The agent loop is not a cognitive cycle. It is an application loop around repeated LLM calls. Each pass prepares input context, gets model output, validates the output, performs any approved side effect, and serializes the result back into the next context.
Agent mechanics
Agents run inside a harness loop
Once you see the loop as context flow, the agent boundary becomes explicit. The model only predicts the next token sequence. The harness owns validation, execution, observation capture, stopping conditions, and budget control.
A stripped-down version looks like this:
// Fixed prefix: instructions, tool definitions, and the current task.
const base = basePrompt(task);
// Accumulated transcript: everything appended so far.
const transcript = [];
let finished = false;
while (!finished) {
// Context is logically an append-only string.
// Each turn rebuilds it from the fixed prefix plus everything
// accumulated so far. The model has no memory of prior turns.
const context = base + transcript.join('');
// The model can emit any token sequence: a final answer, a tool
// request, a reasoning trace, code, or any combination.
const reply = await askModel(context);
// The harness inspects the raw output and decides how to act.
const action = checkModelReply(reply);
if (action.type === 'final') {
return action.message;
}
// Everything the model produces is appended to the transcript.
transcript.push(reply);
// If the model requested a tool, run it and append the observation.
if (action.type === 'tool_call') {
const observation = await runTool(action);
transcript.push(observation);
}
// Stop if the work is done, the step budget is exhausted,
// or the context window is full.
finished = shouldStopNow(task, transcript);
}
There is no hidden agent substance here. The useful parts are the pieces around the model: context building, output checking, tool execution, result capture, and the decision to keep going.
The loop is the skeleton. What flows through it each turn is context.
Next: Context and Memory