Session 2
AI 3010, advancedBuilding an agent from scratch
Build an agent by hand, with no framework, so that every framework that follows is recognisable rather than magic. Participants called a model running on their own machine through its API, then added the parts of a framework's core one step at a time: memory, a system prompt, tools and the reasoning loop, and approval by a person before the agent acts. The steps come together as a reusable harness and a disruption assistant for a fictional airline, with a bonus on retrieval over EU passenger-rights law.
60 minutes
Homework due before your sitting of session 3 on 6 November 2026
Recording
This browser blocks the SharePoint player, even when you are signed in.
Watch the recording in SharePointOpen in SharePoint — use this if the player above is blank, or asks you to sign in.
Captions and a transcript are available in the SharePoint player.
Homework
Due before your sitting of session 3 on .
- Task
- Implement a simple version of the agent you designed on your Agent Canvas in session 1, using a harness written from scratch. You can start from the course repository and adapt it as much as you like. You will probably not have access to every system your agent needs, such as ServiceNow, so mock them, and have your coding agent, such as GitHub Copilot or Kiro, generate the mock data. At the start of session 3, volunteers will demonstrate their agent, and presenting earns an extra entry in the draw.
- Deliverable
- Your code, as a zip file.
- File name
{lastname}-{firstname}-AI3010-Week2.zip- Submit to
- Homework submissions folder
Every submission is entered into a draw for exclusive program swag, awarded at the end of the semester.
Notes
Session 1 designed an agent on paper. This session built one by hand, in Python, with no framework: a model running on your own machine, called through its API, and wrapped step by step in the code that turns it into an agent. Nobody should write agents this way in practice, since frameworks such as LangChain do all of it for you. The point is to see what those frameworks are doing, much as writing a page in HTML by hand, or a web server on raw sockets, teaches you what the tooling later hides.
The lesson worth keeping is a short one. At its simplest, an agent is a call to a model’s API, passing context back and forth as text. Memory, instructions, tools, the loop and approvals are all the harness deciding what goes into that call.
Homework from session 1
Each session opens with a participant presenting their Agent Canvas. In the Western sitting it was an agent for requesting Azure subscriptions: a request raised in ServiceNow, an approval workflow, and resources created to the naming and tagging conventions, with an email to the requester when it is done. Its designer called it closer to a workflow than an agent, and said the canvas’s questions, such as what makes it different from what already exists and what triggers it, helped them think it through. Other submissions included agents for code coverage, performance reviews and secrets management.
In the Eastern sitting it was a triage agent for NDC support. It picks up tasks through the ServiceNow API, looks for similar issues seen before, and, for a new one, reads the client’s configuration through the Azure SDK, and perhaps the logs in Splunk, to judge whether the fault is in the code, the configuration, the client or elsewhere. It then drafts a reply for the developer on duty. For someone rotating in cold, that could save a day of pulling logs and configuration. Some errors are deliberate, such as a client being correctly rate-limited, and with enough of a pattern those replies could eventually be automated too.
Getting set up
The instructions sent ahead of the session asked for three things:
- Ollama, which runs a model on your own machine and serves it through an API. Any similar tool will do, such as llama.cpp.
- A model. The demonstrations used Qwen 3.5 4B, chosen because it is small enough
to run on most laptops:
ollama pull qwen3.5:4b, thenollama serveto start it. A larger Llama model works too, and knows more, if your machine can carry it. - Python 3.11 or later.
The code is in the course repository,
with a folder per session and a README in each. Reaching it needs your P account and
membership of the amadeus-nav organization on GitHub, which is requested through My
Access; a GitHub Copilot licence alone is not enough, and without the membership the
repository shows a 404. The same code is under the downloads as a zip file. In the
session-2 folder:
python -m venv .venv # python3 if this fails
source .venv/bin/activate # macOS and Linux
.venv\Scripts\activate # Windows
pip install -r requirements.txt
A .env file is optional. The code falls back to Ollama’s address and the Qwen
model, so you need one only to change them. The API key is ignored by Ollama, but it
matters as soon as you point the code at a hosted provider such as Microsoft Foundry,
where you can run much larger models and need to watch what they cost. There is a
C# version of the examples in the repository’s extras folder.
What every framework provides
LangChain, CrewAI, the Microsoft Agent Framework and Google’s Agent Development Kit differ in their details, but each is built from the same four layers:
- The core: an abstraction over the model, instructions, tools, the loop, memory, and approvals by a person. This session built only this layer.
- Orchestration: graphs of steps, much like decision trees; durable state, so that an agent which fails can pick up where it left off; and several agents working together, one delegating to another.
- Capabilities: retrieval, integration with APIs, command-line tools and MCP, structured output for programs and other agents to read, and streaming replies as they are generated.
- Production: an audit trail of what an agent did, evaluations that test it much as unit tests test code, guardrails against misuse and hallucination, and a way to deploy it. Without this layer, putting an agent into production amounts to copying a Python file onto a server.
The core breaks down into six parts:
- The model interface. It is thin, because almost every provider has adopted the OpenAI API. What varies is mostly settings, such as reasoning effort and temperature.
- Instructions, or system prompts, which tell the model how to behave. A repository of system prompts coaxed out of commercial harnesses is worth reading for ideas: Claude Code’s, for example, sets out how it should handle memory, its environment, tool calls and delivering its work.
- Tools: functions the model can ask the harness to run.
- The loop, which follows the ReAct pattern, for reasoning and acting, and nothing to do with the front-end library. The model reasons, acts, observes the result, and repeats until the answer is good enough. It is a close relative of chain-of-thought prompting. A limit on the number of turns is essential, because a loop left to itself can run for hours and cost a great deal.
- Memory. A model remembers nothing between calls, so the harness has to.
- Human in the loop: stopping to ask a person before an action is taken.
The Navigo disruption assistant
The example agent helps frontline staff at Navigo, a fictional low-cost airline, rebook a passenger whose trip has been disrupted. When hundreds of passengers are affected and the staff are few, searching for flights while talking to each customer is slow, stressful and error-prone, and mistakes cost money and reputation. The agent takes on that searching. It analyses the passenger’s situation, uses the loop to find alternatives, suggests them, and rebooks only when a person approves.
It has two triggers. One is a chat, such as a panel beside AppCenter: “the passenger with booking ABC123 has been disrupted; how do we get them to San Francisco as quickly as possible?” The other is a one-off task, which can be automated, for example by watching a queue or a customer service inbox and acting on each message that arrives.
Building it, one step at a time
| Step | Adds | What it shows |
|---|---|---|
| 1 | Calling the model | One HTTP request to the model’s API |
| 2 | Memory | The conversation kept in a list and sent with every request |
| 3 | Instructions | A system prompt that sets the rules and keeps the agent on its job |
| 4 | Tools and the loop | Tools described to the model, run by the harness, within a limit |
| 5 | The agent | A reusable harness, an agent declared on it, and approval before rebooking |
| 6 | Retrieval (bonus) | Answers drawn from EU passenger-rights law, and a tool that calculates |
Step 1, calling the model. The request is a model name, a reasoning effort and a
list of messages, each with a role. The system message sets the scene (“you are a
helpful assistant who understands the airline industry”) and the user message asks
the question (“in one sentence, what is Amadeus IT SA?”). The reply comes back as
JSON, with the answer and details such as how many tokens were used. The same few
lines look almost identical in C#, Java or any other language.
Reasoning effort sets how long the model thinks before answering. Left at its default
of medium, a small model on a laptop can take half a minute or more to reply, and
sometimes times out, so the demonstrations set it to none. With it on, you can watch
the model reason through its answer, running a loop of its own.
Small models also get things wrong, and differently each time. In one run the model described Amadeus as an Italian company; it knew nothing of the company’s executives, where a larger Llama model did. Expect that from any model, and check what it says.
Step 2, memory. A model treats every request as its first. A chat remembers you
because its harness keeps the whole conversation, every message and every reply, in a
list, and sends all of it with each new request. ChatGPT and Claude work this way: it
is the harness that remembers, not the model. Run the step with --forgetful and the
model cannot recall a name you gave it a moment ago; run it without and it can. This
memory lasts only as long as the program does. And because the whole list goes with
every request, the context, and with it the cost, grows with every turn.
Step 3, instructions. The system prompt makes the model a disruption assistant for Navigo’s customer service agents, and sets rules: be brief and factual, use bullet points so as not to overload the person reading, never invent numbers or times, and decline anything unrelated. It is put at the start of the memory, so it goes with every request. Ask what your options are after a cancellation and it helps; ask for a poem about a cat and it declines. In the Eastern sitting it also took the bullet points rather too literally. A system prompt can be talked around, which is how the ones in that repository came to be published.
Step 4, tools and the loop. Three tools, get_booking, get_flight_status and
find_flights, look up mock data in airline_data.py. In practice each would call a
real system, such as the Digital API. Each tool is described to the model in a
schema, giving its name, what it does and the parameters it takes, much as MCP
describes a tool. The schema goes with the request. When the model’s reply asks for a
tool, the harness runs it with the arguments the model chose, adds the result to the
memory, and goes round again, up to a fixed number of turns. Try asking it to look up
booking ABC123 and get the passenger to San Francisco.
Step 5, the agent. The plumbing moves into harness.py, so that each new agent is
a declaration rather than a copy of the code. A tool wraps a function and can be
marked as needing approval. An agent has a name, instructions, tools, a model, a
reasoning effort, a limit on its turns and a trace file: a log of everything it does,
written to disk here where in production it might go to Splunk. Different agents can
use different models. The disruption assistant adds a fourth tool, for rebooking the
passenger, and that one needs approval: the agent can do everything up to the
rebooking, and then has to ask. It answers both triggers, in a chat or as a one-off
task given with --task.
Step 6, retrieval. The bonus brings in EU Regulation 261/2004, the European Union’s law on air passengers’ rights, which sets the compensation owed for a cancellation or delay by the distance of the flight and the notice given. Rather than sending the whole regulation with every request, the code splits it into chunks in memory, and a search tool returns only the passages relevant to a question. That works for one short document. For a large body of content, the chunks would go in a vector database instead.
Asked what a passenger was owed, the model answered inconsistently. So the rules were written into a tool that does the arithmetic, which answers at once and the same way every time; only when asked to explain, or to cite the regulation, does the question go to the model. That is a pattern worth copying: use your coding agent to write deterministic code for anything that must come out the same every time, and keep the model for explanation and judgement.
Why it matters here
This is the same pattern Amadeus uses to call models within New Skies and its processes. Raya was a Navitaire project to give airlines an AI to use with New Skies and Stratos: an agentic RAG system that could be extended with skills, agents and MCP servers, and that also helped to train an airline’s new employees. The same pattern is behind Max for Airlines and Max for Travelers. Becoming familiar with the pattern, by getting the code running and changing it, is the point of the homework.
Where to get help
If you are struggling to get the code running, especially if writing code is not your day job, ask Matthew or Ryan. They are glad to walk you through it. And when part of the code makes no sense, ask GitHub Copilot to explain it: it is good at that.