Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 ยท Projects and Practice

Agent Practice Challenges

Practice building AI agents with hands-on challenges: fix a runaway loop, validate tool arguments, design memory, route requests and write evals, each with a solution to run.

Reading about agents is not the same as building them. This lesson gives you six short challenges that each target a skill from this part of the course: memory, retrieval, multi-agent control, safety and evaluation. Every challenge has a starter you can run and edit, a clear goal and a hint. Try each one before opening the solution.

All code is plain Python with deterministic stand-ins for model calls, so every challenge runs in your browser and has one checkable answer. Edit the starter, run it, and compare with the solution below it.

How to practise

  1. Read the goal and run the starter to see what is wrong.
  2. Change only what the challenge asks.
  3. Make the printed checks say True.
  4. Only then read the solution and compare approaches.

Challenge 1: stop the runaway loop

The agent below repeats the same tool call forever. Add a step limit and a repeated-call detector so it stops with a clear message.

Python Challenge 1: add loop safeguards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint:

Check step > max_steps first, then count how often the last call appears in history.

Solution idea: return "stopped: step limit" when step > max_steps, and "stopped: repeated call" when history.count(history[-1]) >= 3.

Python Challenge 1: solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Challenge 2: validate tool arguments

A model returned arguments for book_flight. Write validate_args that checks required keys, types and that passengers is between 1 and 9. Return a list of problems (empty means valid).

Python Challenge 2: validate arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Challenge 2: solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Returning problems as readable strings lets you pass them back to the model so it can fix the call (see guardrails).

Challenge 3: keep the important message

trim below drops the oldest messages to fit a budget, but it also drops the first user message, which holds the goal. Change it so the first user message is always kept.

Python Challenge 3: pin the goal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Challenge 3: solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

See context windows for more strategies.

Challenge 4: a retrieval threshold

The retrieve function always returns the best match, even when nothing is relevant. Add a minimum overlap so that "Who won the cup?" returns an empty list.

Python Challenge 4: decline when nothing matches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Challenge 4: solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The threshold value depends on your data; tune it with an eval set (see RAG).

Challenge 5: route and fall back

Write route(message) returning "billing", "tech" or "general". The messages "I was double charged", "the app crashes on start" and "hello" must map in that order.

Python Challenge 5: build a router

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Challenge 5: solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice "charged" matches the stem "charge". Real routers use a model or embeddings and still keep a fallback (see multi-agent patterns).

Challenge 6: write evals that catch a bug

The agent shout should upper-case the text but has a bug for empty input. Write at least three eval cases, one of which fails, and print the score.

Python Challenge 6: find the bug with evals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Challenge 6: solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The edge case reveals the surprising behaviour. Decide whether the test or the code is wrong, fix one, and keep the case forever (see evaluating agents).

Bigger ideas to build next

  • A meeting notes agent that extracts action items with owners and due dates, validated against a schema.
  • A personal finance categorizer with human approval for uncategorised items.
  • An incident triage agent that reads logs, searches runbooks and drafts a summary, read-only.
  • A tutor agent that remembers what a learner got wrong and quizzes them later (see long-term memory).

For each: write the requirements, the tools, the risks and ten eval cases before writing any agent code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two safeguards stop a runaway agent loop?

    Hard limits in code guarantee the loop ends.
  2. 2. Why return a list of problems from argument validation?

    Specific problems let the model correct the arguments.
  3. 3. A retrieval function with no threshold always...

    A minimum score lets it return nothing for unrelated questions.
  4. 4. What do you do with a failing eval case?

    Cases are your regression safety net.

Summary

  • Practise with small, checkable tasks: loop limits, argument validation, pinned goals, thresholds, routing and evals.
  • Enforce limits and validation in code, not in prompts.
  • Every challenge connects back to a lesson; revisit it if you got stuck.
  • Plan requirements, tools, risks and evals before building your own agent.

Back to the track: AI Agents