learning-it by example

#01 AI agents and MCP 6.3 min

Local AI Agent in Python: No API Key (Ollama)

Build and run a tool-calling AI agent on your own laptop with Python and Ollama. No API key, no bill.

1. How does the video define an agent?
2. Why does the loop have a step limit of six?
3. In the agent demo, where did the numbers forty two and four come from?
4. What went quietly wrong when the small model sent numbers as text?
5. If a tool raises an error, what should the loop do?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Cut the step limit to one

    Copy the code folder first. In agent.py change MAX_STEPS from 6 to 1. The video shows the model asking for both tools in step one. Before you run it, predict what the last line of output will be. Then run python agent.py and compare.

    A hint

    Count how many times the for loop can go round with this limit, and read what the line after the loop returns.

  2. Make a tool crash on purpose

    In word_count, replace the return line with: raise ValueError('broken'). Predict what the step line for word_count will print and whether the whole program stops. Then run it and compare. Find the lines in run_agent that explain what you see.

    A hint

    Look at the try and except around the tool call, and at what is stored in result.

  3. Remove the integer conversion

    In add, change the return line to: return a + b. The video says a small model may send numbers as text. Predict what the add step line will print if it does, and whether Python raises an error. Run it and compare with what the video says. Put the conversion back afterwards.

    A hint

    Try '17' + '25' at a Python prompt first. Adding two pieces of text joins them.

  4. Give the agent a third tool

    Add a function multiply(a: int, b: int) -> int with a one line docstring that returns int(a) * int(b). Add it to the TOOLS dictionary under the name multiply. Change the question at the bottom so it asks what 6 times 7 is. Predict which step lines you expect, then run it. If the model ignores your tool, decide what you would change.

    A hint

    Ollama builds the tool description from the type hints and the docstring, so write a clear docstring. The dictionary key should match the function name.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

One small local model (llama3.2), two toy tools and a single run: a demonstration, not a test. No permissions, logging or tool limits. The advice pages were read as search results; the output on screen is the evidence.

Up next in AI agents and MCP

More in AI agents and MCP

All AI agents and MCP videos

Related videos