The Raspberry Pi runs the agent loop, tools, and hardware access, while a hosted model like Claude does the reasoning through the API. This is the practical route because the Pi 4 has no NPU and a modest CPU, so it’s slow at local inference but great as an always-on, ~3-6W controller.
Typical things it can do:
Read sensors and control GPIO, relays, and cameras Control smart home devices (Home Assistant, MQTT) Run a Telegram/Discord/Slack bot Monitor files, logs, or websites and act on them Run scheduled tasks (cron or systemd timers)
import subprocess import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from env MODEL = “claude-sonnet-5-5”
SYSTEM = (“You are an assistant running on a Raspberry Pi 4. ” “Use the provided tools to check or control the device. Be concise.”)
--- Tools: keep them narrow and specific ---
def cpu_temp(): with open(“/sys/class/thermal/thermal_zone0/temp”) as f: return f”{int(f.read()) / 1000:.1f} C”
def disk_usage(): return subprocess.check_output([“df”, “-h”, ”/”], text=True)
TOOL_FUNCS = {“cpu_temp”: cpu_temp, “disk_usage”: disk_usage}
TOOLS = [ {“name”: “cpu_temp”, “description”: “Get the Raspberry Pi CPU temperature in Celsius”, “input_schema”: {“type”: “object”, “properties”: {}}}, {“name”: “disk_usage”, “description”: “Get disk usage of the root filesystem”, “input_schema”: {“type”: “object”, “properties”: {}}}, ]
history = []
def ask(user_text): history.append({“role”: “user”, “content”: user_text}) while True: resp = client.messages.create( model=MODEL, max_tokens=1000, system=SYSTEM, tools=TOOLS, messages=history) history.append({“role”: “assistant”, “content”: resp.content}) if resp.stop_reason != “tool_use”: return "".join(b.text for b in resp.content if b.type == “text”) results = [] for b in resp.content: if b.type == “tool_use”: try: out = TOOL_FUNCSb.name except Exception as e: out = f”Error: {e}” results.append({“type”: “tool_result”, “tool_use_id”: b.id, “content”: str(out)}) history.append({“role”: “user”, “content”: results})
if name == “main”: while True: print(ask(input(”> ”)))
Another option: Fully local LLM (offline/private)
With llama.cpp or Ollama on 64-bit Raspberry Pi OS, expect:
4GB RAM: 1B-3B models at 4-bit quantization (Llama 3.2 1B/3B, Qwen 2.5 1.5B/3B, Gemma 2B), roughly 2-8 tokens/sec 8GB RAM: up to ~7B models, but only about 1-3 tokens/sec
This is fine for simple commands and classification, but small models are unreliable at multi-step tool use. It’s not a great fit for complex agents.
Fully local LLM on a Pi 4
What limits it
The Pi 4 (Cortex-A72, 4 cores, no NPU, no GPU acceleration for LLMs) is limited mainly by memory bandwidth, roughly 4 GB/s in practice. Each generated token has to read the whole model from RAM, so speed is roughly bandwidth ÷ model size. A 0.8 GB model gives about 5 tokens/sec, while a 4 GB model gives about 1.
Prompt processing (prefill) is also slow, perhaps 10-30 tokens/sec on a 1B model. That matters for agents, because the tool definitions and chat history are re-read on every call.
Realistic model choices
Sizes below are 4-bit (Q4_K_M) quantizations.
| Model size | RAM used | Gen speed | Good for |
|---|---|---|---|
| 0.5-1B (Qwen2.5 0.5B/1.5B, Llama 3.2 1B, Gemma 3 1B) | ~0.5-1.2 GB | 4-8 tok/s | Command parsing, classification, short replies |
| 3-4B (Llama 3.2 3B, Qwen2.5 3B, Qwen3 4B, Phi-3.5 mini) | ~2-2.8 GB | 1.5-3 tok/s | Simple Q&A, basic tool calls |
| 7-8B | ~4.5-5 GB (8GB Pi only) | ~1 tok/s | Too slow for interactive use |
Newer small models come out often, so check the Ollama library for current options. The size tiers above stay the same, though.
Setup with Ollama (easiest)
Use 64-bit Raspberry Pi OS Lite, ideally on a USB SSD:
curl -fsSL https://ollama.com/install.sh | sh
ollama run llama3.2:1b
Test a few models and measure with ollama run <model> --verbose, which prints tokens/sec.
Limit memory use and context size, since a large context eats RAM and slows prefill:
sudo systemctl edit ollama
[Service]
Environment="OLLAMA_KEEP_ALIVE=30m"
Environment="OLLAMA_NUM_PARALLEL=1"
Environment="OLLAMA_MAX_LOADED_MODELS=1"
sudo systemctl restart ollama
Then request a small context (e.g. num_ctx: 2048) in your API calls.
Alternative: llama.cpp (more control, a bit faster)
sudo apt install -y build-essential cmake git
git clone https://github.com/ggml-org/llama.cpp && cd llama.cpp
cmake -B build && cmake --build build -j4
./build/bin/llama-server -m model.gguf -c 2048 -t 4 --port 8081
Download GGUF files from Hugging Face. llama-server exposes an OpenAI-compatible API on localhost:8081.
Using it as an agent
Ollama supports tool calling for some models (Llama 3.2, Qwen2.5, Qwen3). Adapting the earlier agent is a small change:
pip install ollama
import ollama
TOOLS = [{
"type": "function",
"function": {
"name": "set_led",
"description": "Turn the LED on or off",
"parameters": {
"type": "object",
"properties": {"state": {"type": "string", "enum": ["on", "off"]}},
"required": ["state"],
},
},
}]
msgs = [{"role": "user", "content": "turn the light on"}]
resp = ollama.chat(model="qwen2.5:3b", messages=msgs, tools=TOOLS,
options={"num_ctx": 2048})
for call in resp.message.tool_calls or []:
print(call.function.name, call.function.arguments)
Making small models work as agents
Small models fail at multi-step reasoning, so design around that:
- Few tools, 3-5 at most. Every tool definition adds prompt tokens and confusion.
- Strict schemas. Use
enumand short descriptions. Avoid free-text arguments. - One step at a time. Don’t expect “check the temperature, then decide, then act” chains. Put the logic in Python and use the model only to map speech/text to one action.
- Short system prompt, and trim history aggressively.
- Validate outputs in code before acting. Treat the model’s tool call as a suggestion.
- Constrain output with JSON schema or grammars (supported in llama.cpp) when you need guaranteed-valid output.
Hardware tips
- Cooling: a fan or large heatsink is basically required. Inference pins all 4 cores and the Pi 4 throttles at 80°C.
- Power: use a proper 5V/3A supply. Under-voltage causes throttling and crashes.
- Overclock (optional): 2.0 GHz gives a modest speedup. Add
over_voltage=6andarm_freq=2000to/boot/firmware/config.txt, with good cooling. - Swap/zram: helps avoid out-of-memory crashes on the 4GB model, but a model that spills into swap is unusably slow. Pick a model that fits in RAM.
- Storage: a USB SSD loads models much faster than an SD card.
When local makes sense
Good fits are privacy-sensitive data, no internet, no per-token cost, and simple voice commands. Poor fits are open-ended conversation, coding, and anything needing reliable multi-step reasoning. For those, the cloud model (Option 1) is far better, and the hybrid setup (local wake word and speech-to-text, cloud reasoning) is usually the best compromise.