HeadlinesBriefing favicon HeadlinesBriefing.com

Build a Local CLI Agent with Python & Ollama

Towards Data Science •
×

CLI AI agents combine LLM power with direct terminal access, letting you run commands using natural language without a web interface. After Chat GPT’s 2022 release, 2023 OpenAI function‑calling, and 2025 Super CLI, agents like Claude Code and Hermes fit cleanly into developers’ workflows. They augment the shell instead of replacing it, turning the terminal into a collaborative workspace where the AI executes tasks.

This guide shows how to build a fully‑local CLI agent in Python using Ollama. First, install Ollama 0.6.2 and download the light‑weight Qwen model from the website. Define a helper function `execute_shell_command` that runs shell commands via `subprocess`, map it to the tool schema, and provide the JSON schema for Ollama’s tool calling.

The agent starts with a system prompt: “You are a helpful local CLI assistant…”, then loops over user input, sends the conversation to `ollama.chat()` with the tool schema, processes any tool calls, and returns the final text. Running the script lets the model translate natural‑language requests like “How much disk space do I have left?” into `df -h` and safely report the output.

Remember that ambiguous deletion commands can be destructive; double‑check tool parameters before execution.