Ollama and Pydantic AI: build a Python local agent
Learn to connect Python to Ollama on localhost port 11434 and give an agent tools to save notes, read files and check the time.
Player not loading? Watch on YouTube
This tutorial builds a local AI agent in Python with Ollama and Pydantic AI. The speaker starts with Ollama installation, downloads a model, then tests a terminal chat before connecting it to a Python script.
Model choice depends on available memory. The speaker advises checking dedicated GPU VRAM on a Windows machine or unified memory on a Mac, and choosing a model that fits. He recommends Qwen 3.5 4 billion parameters as a starting point and uses a 2 billion parameter variant for the terminal test. If responses are too slow, he suggests a smaller model. His speed observations apply to the demonstrated setup rather than every computer.
The Python portion requires Python and the Pydantic AI library, which the speaker says can be installed with pip or uv. In agent.py, he configures the Ollama model and provider to connect to the inference server on localhost port 11434. Python functions supply tools for checking the time, calculating expressions, and saving or reading a note file.
The agent combines a model, tools and a system prompt. A terminal loop passes user input and message history to the agent, prints responses, and stops on quit or exit. The demonstration includes saving a note, reading it back and requesting the date and time. The walkthrough focuses on a basic script that calls tools on the user's computer.