# Building CLI AI Agents with Python & Ollama

Create a local AI assistant that runs in your terminal

- **Source:** [Towards Data Science](https://towardsdatascience.com)
- **Published:** 2026-08-03
- **Topic:** [Data & AI](https://brifing.si/c/data-ai)
- **Original article:** <https://towardsdatascience.com/cli-agents-with-python-ollama/>
- **On Brifing:** https://brifing.si/s/21d5a6cb-58db-4a20-813c-3e6094a03d53

## What are CLI AI Agents?

CLI AI agents are tools that allow developers to manage terminal operations using natural language. This trend started with ChatGPT's release in 2022 and evolved with OpenAI's tool calling function in 2023. By 2025, specialized agents like Super CLI emerged, running entirely in terminal. These agents boost developer productivity without disrupting existing workflows, making them valuable additions to developer toolkits.

## Setting Up Ollama

To build a CLI agent, you first need to install the Ollama library using 'pip install ollama==0.6.2'. Then download the Ollama application from its official website and use terminal commands to install your preferred open-source LLM. The example uses Alibaba's Qwen model because it offers a good balance between performance and resource requirements, making it ideal for local development environments.

## Executing CLI Commands in Python

The agent needs Python's subprocess module to execute terminal commands. This built-in module lets you run system commands directly from Python code. The example defines an 'execute_shell_command' function that runs given commands and returns their output. It captures both successful outputs (stdout) and errors (stderr), providing comprehensive feedback to users about each command's execution.

## Tool Schemas for Ollama

To use Ollama's tool calling feature, you must map Python functions to JSON schemas. The 'TOOLS_SCHEMA' structure defines which functions can be called and their parameters. For instance, it specifies that 'execute_shell_command' requires a 'command' parameter of string type. These schemas enable the LLM to understand how to properly call your Python functions with appropriate arguments.

## Message Roles in AI Interaction

The AI interaction involves three message roles: system, user, and assistant. The system message provides initial instructions defining the agent's behavior. User messages come from terminal inputs, while assistant messages are the model's responses. In our example, the system message establishes the agent as a helpful CLI assistant capable of using tools to inspect systems and run tasks.

## Tool Calling Mechanism

When the model wants to use a tool, it returns the request in JSON format. The code captures this call and executes the corresponding Python function. The function's result is added to the model's context with a 'tool' role. This allows the model to see tool outputs and generate more informed responses. For example, you can run terminal commands and feed their results back to the model.

## Interaction Loop and Safety

The code runs in a continuous loop, taking user input and passing it to the model. Responses can be either text or tool calls. Given the terminal's sensitive nature, all model actions are verified first. Users can exit by typing 'exit' or 'quit', or use Ctrl+C keyboard combination for safe termination. These safety measures prevent accidental execution of harmful commands.

## The Future of CLI Agents

CLI AI agents have potential to revolutionize developer experience. They transform terminals from simple command interfaces into intelligent collaborators. Particularly valuable for automating complex workflows and debugging, they save significant development time. With open-source tools like Python and Ollama, this technology is now accessible to everyone, marking a new era in developer productivity tools.

## Sources covering this story

- https://towardsdatascience.com/cli-agents-with-python-ollama/ — 

---

_Summary written by Brifing from the original reporting. Copyright in the original article belongs to its publisher; read the full piece at the link above._
