← the late compiler
C_000015 · ai agents · foundation

Agent Loop

The cycle at the heart of every agent: receive context, decide an action, execute it, observe the result, repeat until done.

Step 1 of 5

In words

What it is, why it matters, and what it is like.

Why am I learning this?

This concept unlocks the ability to understand and build AI agents — systems that don't just answer a question but actually do things: book a flight, update a spreadsheet, run a diagnostic. If you master the agent loop — the cycle of receive context, decide, act, observe, repeat — you'll be able to reason about any agent framework (LangChain, AutoGPT, your own code) because they all run on this same loop. From here you'll move on to agentic workflows, multi-agent systems, and agent memory. Without this, it's impossible to understand how an agent 'figures out' a problem step by step.

The idea, in plain terms

Think about how you solve a multi-step problem in real life — say, planning a holiday. You don't just write down the final answer. You start with a goal (book a trip), look at what you have (dates, budget, preferences), decide your first action (search for flights), then you look at the results (flight options appear), and based on that you decide the next action (filter by price), and so on. Each step changes your understanding of the problem, and you keep going until you've reached your goal or you give up. An AI agent works exactly the same way. It's not a single question-and-answer exchange like a chat bot — it's a loop. The agent has a goal, it looks at the situation (the context — like a prompt and any information it has gathered), it decides what to do next (which could be 'call a tool' like a calculator or a search engine, or 'produce a final answer'), it does that thing, then it observes the result (the tool's output), and that new information is added to its context, and the cycle starts again. This continues until the agent decides it's done — either because it has achieved the goal, or because it has exhausted its attempts or hit some limit. Every AI agent, no matter how fancy the framework, is running this loop underneath. The magic is that the 'decide' step is done by a large language model, which can reason about the context and choose the next action based on everything it has seen so far. This is different from a traditional program, which follows a fixed set of steps written by a programmer. The agent writes its own steps, dynamically, based on the conversation.

An analogy

The best analogy is a GPS navigation system (or a human driver using a map). You give it a destination (the goal), and it looks at the current location (the context). Then it decides on an action: 'turn left in 200 metres'. You execute that action (you drive). Then you observe the result: the GPS sees you've moved, maybe it recalculates if you missed a turn. Then it decides the next action: 'turn right'. It repeats this cycle until you've arrived at your destination, and then it says 'you have arrived' and stops. The GPS doesn't plan the whole route from start to finish in one go — it might have an initial plan, but it constantly updates based on what it observes (traffic, road closures, your actual position). An agent does the same: it doesn't compute the entire answer at the start; it iterates, observing the results of its actions and adjusting. The analogy breaks down in an important way: a GPS has a fixed set of possible actions (turn left, turn right, go straight) and a clear rule for choosing them (the map and routing algorithm). An AI agent has a potentially unlimited set of actions (it can call any tool, write any text, make any decision), and the 'rules' are learned from data — the LLM decides what to do based on its training, not a hand-coded map. Also, a GPS's observation is precise (its location), while an agent's observation is just text — it might be vague or ambiguous. And a GPS stops when you arrive; an agent's 'done' condition is more complex — it might be a specific output, a max number of steps, or a judgement by the model that the answer is sufficient.

Definition

The agent loop is the cycle at the heart of every AI agent: receive context, decide an action based on that context, execute the action (which may be calling a tool or producing an answer), observe the result, add the result to the context, and repeat until a termination condition is met.

Where this sits

You haven't learned anything yet, so I'll place this in the wider world. This concept is the cornerstone of 'AI Agents', which is a category of AI systems that go beyond simple question-answering. In your library notes, this is the same as the 'Build an AI Agent (From Scratch)' topic — it's the foundation. It connects directly to 'Agent Executors' (the runtime that drives the loop), 'Agent Memory' (what the agent remembers across iterations), and 'Agent Cards' (how the agent describes its capabilities). Right now, you're learning the engine that runs everything.

Signal from the Frontier

Get the next essay on mind, machine, and meaning

Essays at the intersection of AI, philosophy, and Indian governance. No promotional content.

We'll send a one-click sign-in link to confirm. No password needed.

Views expressed are personal and do not represent the Government of India or the Government of Uttarakhand.