I integrated Ollama with Qwen 2.5:3B to replace cloud LLM API calls with local inference in a multi-agent code generation system.
Why Local LLMs?
| Factor | Cloud API | Local (Ollama) |
|---|---|---|
| Cost | $0.003/task | $0 |
| Latency | 15s | 66s |
| Privacy | Cloud | 100% local |
| Offline | No | Yes |
Setup
1. Install Ollama
winget install Ollama.Ollama
ollama --version
2. Pull Qwen 2.5:3B
ollama pull qwen2.5:3b
ollama list
3. Test the API
curl http://localhost:11434/api/generate -d
{
"model": "qwen2.5:3b",
"prompt": "Write a Python function that computes the factorial of n.",
"stream": false
}
Benchmarks
| Model | Speed/Agent | Quality | Verdict |
|---|---|---|---|
| qwen3:0.6b | 9s | Empty output | Unusable |
| qwen2.5:3b | 22s | Valid code | Default |
| qwen2.5:7b | 43s | Better code | Too slow |
Key decision: 3 agents x 22s = 66s fits within 300s timeout.
Integration Code
Zero external dependencies — just Python urllib:
import json
import urllib.request
import urllib.error
OLLAMA_URL = "http://localhost:11434/api/generate"
DEFAULT_MODEL = "qwen2.5:3b"
def generate_code(problem, agent_role="coder", model=None, timeout=120):
model = model or DEFAULT_MODEL
prompt = f"You are an expert Python programmer. Problem: {problem}"
data = json.dumps({
"model": model,
"prompt": prompt,
"stream": False,
"options": {"temperature": 0.2, "num_predict": 2048}
}).encode("utf-8")
try:
req = urllib.request.Request(OLLAMA_URL, data=data, headers={"Content-Type": "application/json"})
with urllib.request.urlopen(req, timeout=timeout) as response:
result = json.loads(response.read().decode("utf-8"))
return result.get("response", "")
except Exception as e:
print(f"Ollama error: {e}")
return None
def extract_code(response):
if not response:
return None
import re
match = re.search(r"```
python\s*\n(.*?)
```", response, re.DOTALL)
if match:
return match.group(1).strip()
return None
System Prompts
Coder
You are an expert Python programmer. Write clean, efficient code.
Use numpy where appropriate. Output ONLY code in a code block.
Reviewer
You are a senior code reviewer. Identify bugs and suggest improvements.
Performance in Practice
Pipeline: Task -> 3 agents process (66s) -> Code saved -> Validation -> Done
End-to-End Test: Cholesky Decomposition
import numpy as np
def cholesky_decomposition(A):
n = A.shape[0]
L = np.zeros((n, n))
for i in range(n):
for j in range(i + 1):
if i == j:
s = sum(L[i, k] ** 2 for k in range(j))
L[i, j] = np.sqrt(A[i, i] - s)
else:
s = sum(L[i, k] * L[j, k] for k in range(j))
L[i, j] = (A[i, j] - s) / L[j, j]
return L
np.random.seed(42)
A = np.random.rand(100, 100)
A = A @ A.T + np.eye(100) * 0.1
L = cholesky_decomposition(A)
recon_error = np.linalg.norm(A - L @ L.T)
print(f"CHOLESKY_VALID: True")
print(f"RECON_ERROR: {recon_error:.6e}")
When to Use Local vs Cloud
| Scenario | Recommendation |
|---|---|
| Cost-sensitive (100+ tasks/day) | Local |
| Privacy-sensitive | Local |
| Quality-critical | Cloud |
| Multi-agent (>3 agents) | Cloud |
| Offline work | Local |
Key Takeaways
- qwen2.5:3b is the sweet spot — 22s/agent, valid code
- Zero dependencies — urllib is enough
- Role prompts matter — different prompts improve quality
- 3 agents for code tasks — fits within timeout
- Fallback chain — external > Ollama > template
Reproducibility
winget install Ollama.Ollama
ollama pull qwen2.5:3b
python ollama_integration.py
Tested on Intel i7-1195G7, 32GB RAM, Ollama 0.5.x.
Top comments (0)