#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.
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.
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.
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.
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
Related videos
- #58 JSON Mode vs Schema vs Function Calling: Measured (Ollama) LLM engineering and evals
- #59 Stream LLM Replies in Python: First Word in 0.2s LLM engineering and evals
- #05 Structured Outputs in Python: Valid JSON Is Not Correct JSON LLM engineering and evals
- #11 Run an LLM Locally with Ollama and Python: First Call Local and open models
