MSBA-370 · Lesson 3 of 12 · 18 min

Agent architecture: models, tools and loops

▶ Video coming soon A video version of this lesson is on the way.

For the third session Priya brought no slides. She opened a terminal, started Copilot CLI in the trail-units repository, asked it to explain why one test was slow, and then, before it answered, asked everyone to predict what it would do first. Malik guessed it would read the test file. Lena guessed it would run the tests. Dmitri guessed it would search for the word "slow". It read the test file, then the package manifest, then ran the single slow test with a timer, then read the source file the test called. "Four decisions," Priya said, "and the model made every one of them from what it saw after the last one. That is the loop. Today we take it apart."

The agent loop

Every agent, however it is packaged, runs some version of the same loop. The controller assembles a prompt containing the system instructions, the custom instructions, the task, the conversation so far and descriptions of the available tools. The model returns either a final answer or a request to call one or more tools, with arguments. The controller checks the request against its permissions, runs the tool, captures the result and appends it to the conversation. Then it calls the model again. The loop ends when the model returns a final answer, when a limit is reached, or when a person stops it.

A loop diagram. Assemble prompt leads to model call. The model either returns a final answer, which exits the loop, or a tool request. A tool request goes to a permission check, which either asks a person, refuses, or runs the tool. The tool result is appended to the context, which leads back to assemble prompt. A limits box beside the loop lists steps, time and cost.
The agent loop. The model decides; the controller checks, runs and records.

Three properties of this loop explain most agent behaviour. First, the model sees only what is in the prompt at that moment; it has no other view of the repository. Second, each tool result makes the prompt longer, so long sessions consume more of the model's context window and cost more. Third, the model's choices are probabilistic: run the same task twice and the agent may read files in a different order, try a different fix, or stop at a different point. That variability is why the success criteria from lesson 2 must be checked by something other than the model.

The model

The model is a large language model trained to produce text, including structured requests to call tools. Copilot offers several models from different providers, and the choice affects quality, speed and cost. Larger reasoning models plan better and recover from errors more reliably but are slower and use more credits; smaller models are faster and cheaper and are often good enough for narrow tasks. GitHub lets users and administrators choose among the models their plan and policies allow, and an auto setting lets Copilot pick a model. Models are added and retired often, so the exam focuses on the trade-offs rather than on any particular model name.

Choosing a model for an agent task. Model names change; the trade-offs do not.
If the task isPreferBecause
Broad, ambiguous or multi-stepA stronger reasoning modelPlanning and error recovery matter most
Narrow and repetitiveA faster, cheaper modelQuality is similar and cost adds up
Long-running in the cloudThe model your organization approved for agentsPolicies and budgets apply per model
Sensitive to latencyA faster model or a smaller scopeEach loop step waits for the model

Instructions

The model's behaviour is shaped by several layers of instructions. The product supplies system instructions that the team cannot see or change, which define the agent's basic role and safety rules. The repository and organization supply custom instructions: files such as .github/copilot-instructions.md, path-specific instruction files and AGENTS.md that describe how to build, test and style the code. A custom agent adds its own role-specific instructions. Finally there is the task: the issue, the chat message or the prompt that says what to do now. Lesson 9 covers how to write custom instructions and custom agents; for now, the point is that every layer ends up in the same prompt and competes for the model's attention.

Tools

A tool is an action the controller knows how to carry out, described to the model by a name, a short explanation and a schema for its arguments. Built-in tools in Copilot's agents include reading and editing files, searching the workspace, running terminal commands and fetching web pages. More tools arrive through MCP servers, covered in lesson 5. The model never runs anything itself. It writes a request, and the controller decides whether and how to carry it out. Lesson 4 covers tool design and function calling in detail.

The environment

The environment is where tools act: the files they can see, the commands available, the network they can reach and the credentials present. Agent mode uses the developer's own machine, which is convenient and risky, because the agent can reach anything the developer can. The cloud agent uses a fresh, temporary environment built on GitHub Actions, with a firewall and only the access it is given, which is safer and easier to reproduce. Lessons 6 and 7 cover environments and sandboxes.

The controller and its limits

The controller is where most of the engineering lives. It decides which tools to offer, enforces permissions, asks people for approval, truncates or summarizes long tool outputs to keep within the context window, retries failed tool calls, and stops the loop when limits are reached. Typical limits are a maximum number of steps, a maximum running time and a maximum cost. Copilot's controllers apply such limits for you; when you build your own agent, for example with the Copilot SDK or another framework, you have to set them yourself.

Worked problem

Reading a loop from a log

Malik opened the session log for a cloud agent task that added a missing input check to the trail-units action. It showed: read action.yml; read src/index.js; search for "units"; edit src/index.js; run npm test (2 failures); read test output; edit src/index.js; run npm test (pass); run npm run build; commit; finish. How many loop iterations were there, and where did the model change its mind?

Show solution
  1. Each tool call is one iteration, so there were ten tool calls before the final answer.
  2. The model changed course after the first test run, when the result showed two failures.
  3. It read the output, made a second edit and reran the tests, which is the decide-act-observe loop working as designed.
  4. Running the build before committing came from the repository instructions, which said the bundled dist folder must be rebuilt.

Single-shot, workflow or agent

Not every problem needs a full agent, and the GH-600 outline rewards knowing when not to use one. A single-shot model call, such as asking for a commit message from a diff, is cheapest and most predictable. A workflow is a fixed sequence of steps, some of which call a model, such as a GitHub Actions job that summarizes a failing log and posts a comment. An agent decides its own steps and is appropriate only when the path cannot be known in advance. Seraphine summed up Skerry's rule: start with the simplest option that works, and add autonomy only when a fixed path cannot do the job.

Why agents fail

Knowing the architecture makes failures easier to diagnose, which is a skill MSBA-371 develops for the evaluation domain. Failures fall into a few families. Reasoning errors are wrong decisions by the model, such as misreading the task. Tool misuse is calling the wrong tool or the right tool with wrong arguments. Context problems happen when the model lacks information it needed, or has so much that the important part is lost. Environment problems are missing dependencies, blocked network access or absent permissions. Each family has a different fix: clearer instructions, better tools, better context, or a better environment. Changing the model is rarely the first fix.

Context: what the model sees

The model never sees the repository directly. It sees only the context: the text the controller assembles for each call. That text usually contains the system instructions, the repository's custom instructions, the task, a summary of earlier steps and the results of recent tool calls. Everything else, every file not opened and every command not run, is invisible to the model. When an agent makes a strange choice, the first question to ask is what was in its context at that moment, and the session log is usually where the answer is found.

Context has a limit, measured in tokens, and long sessions approach it. Controllers handle this in different ways: they drop old tool results, summarize earlier steps, or keep only the most recent part of the conversation. Each method loses something. An agent that has summarized away the detail of a test failure may repeat a fix it already tried. MSBA-371 covers memory and state in depth; for now the point is that the loop is not just a cycle of calls but a cycle of choosing what the model gets to see.

Determinism and repeatability

Run the same task twice and an agent may take different paths. Models produce varied output, tool results change as files change, and small differences early in a session grow into larger ones later. That is not a fault to be eliminated so much as a property to design around. Tasks with clear done criteria and good checks produce acceptable results by different routes. Tasks without them produce results that differ in ways nobody notices until later. The GH-600 outline's emphasis on success criteria and evaluation follows directly from this.

Teams that need repeatable results use the controls they already have. A fixed workflow gives the same sequence of steps every time. Checks in CI judge the result the same way every time, whatever route produced it. Instructions narrow the choices the model is likely to make. And plans approved by a person fix the approach before execution begins, so that two runs of the same plan differ only in details.

Reading the loop in a session log

Malik practised reading cloud agent session logs as a sequence of loop turns. Each turn showed what the agent intended, which tool it called with which arguments, and what came back. Reading a dozen logs taught him to spot the common shapes: a short, direct session where each tool result led straight to the next step; a wandering session that opened many unrelated files; and a stuck session that ran the same failing command several times with small variations. The first shape usually meant a good task. The other two usually meant something missing from the task, the instructions or the environment.

Reading session logs for patterns.
Pattern in the logLikely causeFix to try
Opens many unrelated filesTask does not name the areaName files or folders in the task
Retries the same commandMissing tool or dependencyAdd it to copilot-setup-steps
Rewrites working codeUnclear scopeState what is out of scope
Stops early with a guessNo way to check the resultAdd or point to tests

Limits as a design choice

The limits on a loop are not just safety stops; they also shape behaviour. A tight limit on steps pushes the task designer to write narrow tasks. A time limit protects a shared environment from a session that never ends. A cost limit makes a runaway session visible as a budget alert rather than a surprise at month end. Products set their own limits, and some can be adjusted by administrators. Priya's advice was to treat a session that hits a limit as information: usually the task was too broad or the environment was missing something, and raising the limit would only make the same failure more expensive.

Common mistakes

  • MistakeAssuming the agent can see the whole repository at once.

    InsteadIt sees only what is in the prompt; it must read files through tools.

  • MistakeExpecting the same path every time.

    InsteadAgent behaviour is probabilistic; verify outcomes, not steps.

  • MistakeUsing an agent when a fixed workflow would do.

    InsteadStart with a single call or workflow; add autonomy only when the path cannot be fixed.

  • MistakeSwitching models as the first response to failure.

    InsteadDiagnose whether the cause is reasoning, tools, context or environment.

Summary and key terms

  • The agent loop assembles a prompt, calls the model, checks and runs tool requests, appends results and repeats until done or stopped.
  • The model chooses; the controller enforces permissions and limits on steps, time and cost.
  • Instructions come in layers: system, custom, custom agent and task, all in one prompt.
  • The environment determines what tools can reach; cloud environments are temporary and restricted.
  • Use a single call or a fixed workflow when possible; failures come from reasoning, tools, context or environment.
Context window
The maximum amount of text a model can consider in one call.
System instructions
Instructions supplied by the product that define the agent's role and safety rules.
Custom instructions
Instructions written by a team, in files such as copilot-instructions.md and AGENTS.md.
Environment
The machine, files, network and credentials where an agent's tools act.
Workflow
A fixed sequence of steps, some of which may call a model.
Tool misuse
A failure in which an agent calls the wrong tool or passes wrong arguments.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. What does the controller do with a tool request from the model?

    Show answer

    Checks it against permissions, runs it or asks a person, and appends the result to the conversation.

  2. Name three typical controller limits.

    Show answer

    Maximum steps, maximum time and maximum cost.

  3. When is an agent the right choice over a workflow?

    Show answer

    When the steps cannot be known in advance.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz