Agent lab: write an AI agent loop with tools, with a stand-in model
This lab gives each student an AI agent written by hand in Python, to read, run and change: the loop that sends a conversation and a list of tools to a language model, runs the tool the model asks for and sends back what it returned. They learn tool schemas, stopping conditions and a step limit, errors and retries, memory, planning a task as several turns, guardrails and a trace that is scored.
The model is a stand-in, and the lab says so everywhere: it answers in the same format as a real model API, but it chooses every reply by fixed rules and calls one tool a turn, so the same question always gets the same answer. Nothing is downloaded, no AI service is called and nothing costs money. The tools are pretend too, working on fixed data, and every copy has no route to the internet and none to any other student.
What students learn
What an agent is underneath. They see a model answer with a tool call instead of words, run the tool themselves and send back "Rain, 9 C". They follow the loop through five turns that share 17 messages, see a model forced to call tools stopped by a step limit at 3 steps, and do a three-step plan as three turns. A failing tool comes back as words, and a busy model is asked again after a growing pause. They see an allowed list refuse a delete, and an order hidden in an email obeyed only when the program pastes it in as the user's words. Last, a trace is logged and four test cases score the agent, 3 of 4 passing. It suits a course on software development or applied AI; check your own awarding body's unit for the exact wording, none is quoted here.
How this differs from the AI coding agent labs
The AI coding agent labs give a student a ready agent, opencode, to work with on a program. This lab is the other side: the student reads and changes the agent itself, about 120 lines of Python, so the loop, the tools and the guardrails are code they can see. The LLM workbench lab, about the model API alone, is a good one to do first.
Run it in your browser
The lab starts two services. model is the stand-in, answering at http://model:4000/v1 inside the lab. tools is a Python with agent.py, the agent loop and its pretend tools, llm.py, a small client, and steps/, short scripts that each show one idea. The terminal beside the lab has curl and no Python, so it reaches the scripts with docker exec tools.
- Open the Container page. Open Build on the bar and choose Run a container. Anybody may open it, and the Git lab and the SQL lab start with no account. Every other lab needs a free account: create one, sign in, then press Start.
- Find the lab. The Sample labs card lists every sample. Find Agent lab: write an AI agent loop with tools.
- Press Start. Start runs it as it is, in one press. Edit the file first opens the compose file so you can change it before you start.
- Open it. On the lab's page, press Open terminal and type docker exec tools python3 steps/onecall.py to see one tool call made by hand. Then type docker exec tools python3 steps/loop.py to watch the loop.
What to ask students to hand in
agent.py with one change of their own: a new tool with its schema, a tighter allowed list, or another test case in steps/trace.py. With it, the output of the step that shows the change and a sentence on what a real model might do differently. Nothing is marked automatically: the sentence shows whether it was understood.
A basic lesson on this lab
Agent Loop Basics is a small lesson worked in this lab. In it a student sees a tool call on the wire, runs one by hand, follows the loop and its step limit, does a plan as several turns, handles errors, tries the guardrails and scores a trace.
Free personal accounts have it in the Basics module, which is on Home, and every college can copy it from the Library's Basics folder. It is deliberately small: it shows a teacher what a lesson worked in a lab is, and the lab does the rest. Copy it into a module of your own to change it.
The code on GitHub
The compose file has no Dockerfile: it runs one stock public image, Python 3.12 on Alpine, pinned to an exact tag, twice, each as an unprivileged account with 64 MB of memory. The image is under the PSF licence and is not distributed by us. The file writes the stand-in, the agent and the scripts from fixed text when it starts, with Python's standard library alone, so nothing is downloaded. Nothing is kept when the lab stops, so a student copies their agent.py out first.
Set it as work
Say in the task what to hand in.
- Make the lab. In your module add a task and choose the Container lab card, then In a GitHub repository. Give https://github.com/mitsurutech/lab-agent-lab with the branch main. The compose file is the default, compose.yaml. Fork the repository first if you want to change it, for example to give the agent a tool of your own.
- Publish and try it. Publish pins the commit you were shown, so a later change in the repository does not alter a lab you have already set. Start your own copy first.
- Hand it out. Assign it like any task, with a due date if you want one, and say in the task what to do and what to hand in.
- Mark what is handed in. Open a student’s copy and the work they handed in, mark it, and send it back with a reason if it needs more.
Try this lab, and what it costs
A free personal account can work through the basic lessons in this lab, with enough free credit each month to follow most lessons and no card. For a class, a school or college has its first 365 days free for up to 50 students, then pays per student a year, with every lab included and staff free.
Questions people ask
- Is there a real AI model in this lab?
- No. The model is a stand-in written for the lab, a short Python program that chooses each reply by fixed rules. It answers in the same format as a real model API, tool calls included, so the agent loop students write works the same way against a real one. It always gives the same answer to the same question, which is what lets the lesson say what a student will see.
- Why does the agent call only one tool for a question that needs two?
- The stand-in calls one tool a turn and then reports its answer. A real model may call several in a row. The lab uses this on purpose: one test case fails because of it, and the plan step shows the fix, a big task given to the agent as several turns.
- Is the prompt injection part safe to teach?
- Yes. It is taught as a defence, on pretend data: the inbox is one fixed email and the files are a Python dictionary, so nothing real is read or deleted. Students see why a tool's result must stay data and why code, not the model, decides which tools may run. The stand-in never obeys text in a tool result, and the lesson says a real model might.
- Does it cost anything or use credits for AI?
- No AI is used, so nothing is charged for it. The lab runs like any other container lab.
- Why does the terminal use docker exec tools?
- The lab's terminal carries a few clients, curl among them, and no Python. docker exec tools runs a command inside the lab's Python container, where the agent and the scripts are.