Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

AI Coding Agent Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab7 min read6 commands

A bug in the tuck shop till

The school tuck shop has a small program that adds up a basket. A pupil buys crisps, juice and an apple, and the till says 35p. That is wrong. You could read the code and find the bug yourself. Today you ask an AI coding agent to help, and you check its work.

By the end of this lesson

Note

I can use a coding agent to fix a bug and check the fix myself.

Key words

  • Coding agent: a program that reads and changes code for you.
  • Model: the AI that writes the agent's answers.
  • Prompt: what you ask the agent to do.
  • Test: a small program that checks your code gives the right answer.
  • Permission: the agent asking before it changes something.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The lab is one service called agent, and the terminal reaches it by that name. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. Is the agent ready?

The agent is installed each time the lab starts. This prints installing while that happens and ready when it has finished. Run it again until it says ready.

Command
docker exec agent cat /tmp/agent/status

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

2. Run the tests first

Before anybody changes anything, find out what is wrong. The project has five tests. Four pass and one fails: a basket adds up expected 180 and got 35. Write down which test failed. You will need it later.

Command
docker exec -w /home/node/project agent node --test

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

3. Ask the agent to explain

An agent can read a whole project. Here you ask a question and nothing else. opencode run asks one question and prints the answer. Read it: does it spot that the loop keeps only the last price?

Command
docker exec -w /home/node/project agent opencode run "Explain what the total function in shop.js does, in three short sentences. Do not change any file."

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think an agent's answer must be right because it sounds sure. It can be wrong, and still sound sure. Trust a change only when you have run the tests yourself and read the line it changed.

4. Let the agent fix it, with your say-so

This opens the agent's own screen in your terminal. Type: Fix the bug in total in shop.js so the tests pass. Do not change shop.test.js. When it asks to change a file, read the change first, then allow it. When it has finished, press Ctrl+C twice to close it.

Command
docker exec -it -w /home/node/project agent opencode

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

5. Check it yourself

Now run the tests again: all five should pass. Then run the till. The basket of crisps, juice and an apple should cost £1.80, and the change from £5 should be £3.20.

Command
docker exec -w /home/node/project agent node --test

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
docker exec -w /home/node/project agent node shop.js

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Questions to answer

  1. Which test failed before the fix, and what did it expect?
  2. Which line did the agent change, and why was the old line wrong?
  3. Why did you run the tests yourself after the agent said it had finished?
  4. What should you never type into a prompt?

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service with an AI coding agent beside it, and its model is the platform's, in London. Key learning points: 1. An agent reads and changes code for you. 2. Run the tests before and after any change. 3. Read every change before you allow it. A pupil who believes the agent because it sounds sure is the mistake to listen for: ask them to show you the test passing. The student hands in the tests before and after, the changed line and their answers. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

About this lab and its source: AI coding agent lab: use opencode to explain and fix code, and check its work

Teaching a class? Set these lessons for your students with marking and a lab for every student.