Local Reasoning

Run QwQ and DeepSeek-R1 reasoning models locally through Ollama on a math task.

local_reasoning.py
"""
Local Reasoning
===============

Demonstrates this reasoning cookbook example.
"""

from agno.agent import Agent
from agno.models.ollama import Ollama
from rich.console import Console


# ---------------------------------------------------------------------------
# Create Example
# ---------------------------------------------------------------------------
def run_example() -> None:
    console = Console()

    # Test task
    task = "What is 23 × 47? Show your step-by-step reasoning."

    console.rule("[bold cyan]Local Reasoning with Ollama[/bold cyan]")

    # Test with QwQ (Alibaba's reasoning model)
    console.print("\n[bold blue]QwQ:32B (Alibaba Reasoning Model)[/bold blue]")
    console.print("[dim]Running locally with complete privacy...[/dim]\n")

    try:
        agent_qwq = Agent(
            model=Ollama(id="qwq:32b"),
            markdown=True,
        )
        agent_qwq.print_response(task, stream=True, show_full_reasoning=True)
    except Exception as e:
        console.print(f"[red]Error: {e}[/red]")
        console.print(
            "[yellow]Make sure Ollama is running and qwq:32b is installed:[/yellow]"
        )
        console.print("  ollama pull qwq:32b")

    # Test with DeepSeek-R1:8B (smaller, faster)
    console.print("\n[bold green]DeepSeek-R1:8B (Smaller, Faster)[/bold green]")
    console.print("[dim]Running locally with complete privacy...[/dim]\n")

    try:
        agent_deepseek = Agent(
            model=Ollama(id="deepseek-r1:8b"),
            markdown=True,
        )
        agent_deepseek.print_response(task, stream=True, show_full_reasoning=True)
    except Exception as e:
        console.print(f"[red]Error: {e}[/red]")
        console.print(
            "[yellow]Make sure Ollama is running and deepseek-r1:8b is installed:[/yellow]"
        )
        console.print("  ollama pull deepseek-r1:8b")


# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    run_example()

Local routing and visible thinking

Add host="http://localhost:11434", api_key=None to every Ollama(...) constructor in the saved program, and clear OLLAMA_API_KEY using the setup step below in both the model-pull and Python terminals. These settings select the local server explicitly; a cloud key otherwise changes Agno's default host.

The current adapter does not copy Ollama's separate message.thinking field into Agno's reasoning output. A separate Ollama reasoning stage can therefore pass its answer text as a fallback. The model may still think internally even when Agno displays no reasoning content. Local model inference describes where the model call runs; it does not by itself disable Agno telemetry or prove that every part of an application stays offline. See telemetry.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno ollama

Select the local Ollama server

In the shell used for the pull commands and Python example, clear a previous cloud key and point native clients and embeddings at your local server:

unset OLLAMA_API_KEY
export OLLAMA_HOST="http://localhost:11434"

Without this reset, OLLAMA_API_KEY makes Agno's default Ollama model route to https://ollama.com even when OLLAMA_HOST points locally. Keep a local Ollama server running for the following steps.

Prepare Ollama

Install and start Ollama, then pull the models used by this example:

ollama pull deepseek-r1:8b
ollama pull qwq:32b

Run the example

Save the code above as local_reasoning.py, then run:

python local_reasoning.py

Full source: cookbook/10_reasoning/models/ollama/local_reasoning.py