Starts Nov 23Live Cohort 3 of Vibe Coding & AI Agents (Nov 23, 18:30 MSK): 3 weeks, 9 live hands-on sessionsCohort 3 (Nov 23) →
ReleaseAPI / Product · ActionableMachine Learning Mastery→ original

Local AI Agent in Python: Qwen3 4B and Ollama Search Your Files Without an API Key

For a neural network to work with files on your computer, it needs tools. A Machine Learning Mastery tutorial shows how to build such an assistant in Python: the Qwen3 4B model, served through Ollama, asks on its own to find, read, or remember data in the project folder. Your script executes the code, and the result goes back to the model. Everything runs locally without an API key, and the model download is about 2.5 GB.

AI-processed from Machine Learning Mastery; edited by Hamidun News
Local AI Agent in Python: Qwen3 4B and Ollama Search Your Files Without an API Key
Source: Machine Learning Mastery. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceMachine Learning Mastery ↗
Editor-in-Chief
Key metric #1The default context is 4K tokens when VRAM is under 24 GiB; the example uses 32K.
Five toolsfile listing, search, reading, recent changes, and memory.
Key metric #3The response from any tool is capped at 20 matches and 3,000 characters.

Most AI agent demos rely on paid APIs and the cloud. But for practical tasks like "find where this parameter is described in the project," a small local model is often enough if you give it the right tools. Machine Learning Mastery has published a detailed tutorial on building a project-folder assistant with Python, Ollama, and Qwen3 with 4 billion parameters. The model, the files, and the conversation all stay on your machine.

How the model "uses" tools

The key detail: the model doesn't run anything itself. When it needs an action, it replies with a structured request containing the tool name and its arguments. Your code runs an ordinary Python function and sends the result back to the model in a separate message with the role tool. A conversation has four roles: system sets the behavior, user is the human's question, assistant is the model's reply or tool request, and tool is the result.

Within a single turn, the loop can repeat many times. For example, the model searches for a word, reads the matching fragment, searches again, and only then answers. One reply can contain several tool calls at once, and each one needs its own result. Wrap this in a loop and you have the foundation of an agent.

The ollama library builds the tool description from the function name, type hints, and the docstring. The model picks a tool based only on that description, so the docstring matters most: it has to say what the function returns and describe each argument.

Five tools and guardrails against overreach

The assistant answers questions about a small Python project: code, README, changelog, notes. The model has five tools: list_files shows the folder contents, search_files looks for lines containing a word or phrase, read_file reads a range of lines, recent_files shows files changed in the last few days, and remember saves a fact for future sessions.

The author builds in several engineering rules.

  • The model writes the paths, so each one is validated against the root folder. Attempts like ../../secrets.txt or an absolute path are rejected. Hidden directories and symlinks pointing outside the root are skipped.
  • Only remember writes anything, and it writes to one small file in the home directory, outside the project. Search won't find it, and the other four tools are read-only.
  • The size of every response is limited. In the example, that means no more than 20 matches, lines trimmed to 160 characters, reads returning 80 lines by default, and an overall result limit of 3,000 characters. A single call can't fill the context window.
  • Errors come back as plain text, for example "that path is outside the folder." That gives the model a chance to correct itself.

Search is deliberately simple: it's a case-insensitive substring match, not a regular expression. For a small model, this makes it easier to call the tool correctly. Each match arrives in the format "path:line: text," so the model can point to a specific location. When the limit is hit, the function adds a hint to narrow the query, so the limit itself suggests the next step. read_file ends with something like "[lines 15-22 of 22]," so the model knows whether there is more to read.

Context and hardware: where to save

The most down-to-earth part of the material is context configuration. Ollama picks the context length based on available video memory, and on cards with less than 24 GiB of VRAM the default is only 4K tokens. That's too small for agents: tool descriptions, earlier messages, and tool results all land in the context. Ollama recommends at least 64K tokens for agentic workloads.

Qwen3-4B has a native context of 32,768 tokens, so the author sets 32K by launching OLLAMA_CONTEXT_LENGTH=32000 ollama serve. That's enough for this assistant, because tool results are capped and only the last six messages are kept in conversation memory. The larger the window, the more memory it needs. After loading, run ollama ps and check the PROCESSOR column to confirm the model is on the GPU rather than falling back to the CPU. If your hardware can't handle 32K, use the largest context your system sustains and keep tool responses short.

What this means in practice

The recipe is useful because it shows a pattern rather than a specific model: narrow tools, hard output limits, path validation, and clear plain-text errors. The same skeleton can be adapted to other file formats by adding extensions to the TEXT_SUFFIXES list, and to other models that support tool calling.

There are limitations too. A four-billion-parameter model is weaker than cloud models, which is why the author chooses primitive search and short descriptions. The 64K tokens Ollama recommends are out of reach for this setup, and if VRAM is short, the context has to be trimmed. The source text breaks off in the description of recent_files, so we don't cover the agent loop implementation or long-term memory here. The author has published the full code in a GitHub repository.

Story

Key Questions Answered

How the model "uses" tools?

The key detail: the model doesn't run anything itself. When it needs an action, it replies with a structured request containing the tool name and its arguments. Your code runs an ordinary Python function and sends the result back to the model in a separate message with the role tool. A conversation has four roles: system sets the behavior, user is the human's question, assistant is the model's reply or tool request, and tool is the result. Within a single turn, the loop can repeat many times.

What are the key details on Five tools and guardrails against overreach?

The assistant answers questions about a small Python project: code, README, changelog, notes. The model has five tools: list_files shows the folder contents, search_files looks for lines containing a word or phrase, read_file reads a range of lines, recent_files shows files changed in the last few days, and remember saves a fact for future sessions. The author builds in several engineering rules. The model writes the paths, so each one is validated against the root folder.

What are the key details on Context and hardware: where to save?

The most down-to-earth part of the material is context configuration. Ollama picks the context length based on available video memory, and on cards with less than 24 GiB of VRAM the default is only 4K tokens. That's too small for agents: tool descriptions, earlier messages, and tool results all land in the context. Ollama recommends at least 64K tokens for agentic workloads. Qwen3-4B has a native context of 32,768 tokens, so the author sets 32K by launching OLLAMA_CONTEXT_LENGTH=32000 ollama serve.

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Hands-On AI Engineering · Hamidun Academy

Build & Ship Production AI Agents Without Boilerplate Code

Start your free 72-hour trial at Hamidun Academy (academy.hamidun.com) — interactive lessons on Claude Code & Cursor, live Tech Radar, and production agent templates.

Start 72h Free Trial →Vibe-Coding Intensive
What do you think?
Loading comments…