Table of Contents
- Objectives
- The two agents
- Setup
- Connect to llama.cpp
- Connect to Ollama
- Connect to OpenRouter
- Guided usage
- Most used functionalities
- Example 1 — Python coding
- Example 2 — Managing files and local problems
- Example 3 — Local documentation
- Pi: https://pi.dev
- OpenCode: https://opencode.ai
A terminal coding agent reads your project, edits files, runs commands and answers questions — all inside a TUI. This lab installs Pi and OpenCode, wires them to local models (llama.cpp, Ollama) and to a cloud gateway (OpenRouter), and walks through three practical sessions.
Objectives
By the end of this session you should be able to:
- Install both agents on Linux and Windows (native and WSL).
- Point the agents at llama.cpp and Ollama using OpenAI-compatible endpoints — no MCP, no RAG.
- Use OpenRouter as a single-key gateway for cloud models.
- Run a typical session: init, ask, plan, execute, review diffs.
- Apply the agents to three real tasks: Python coding, file housekeeping, and local documentation.
The two agents
| Pi | OpenCode | |
|---|---|---|
| Site | pi.dev | opencode.ai |
| Interface | TUI | TUI (+ desktop and web apps) |
| Philosophy | Minimal core; extensions, skills, packages | Batteries included; LSP integration, sessions |
| Model switching | /model or Ctrl+L; favorites with Ctrl+P | /models picker |
| Auth | /login or models.json | /connect or opencode.json |
| Providers | Anthropic, OpenAI, Google, OpenRouter, Ollama, and more | 75+ providers, plus any OpenAI-compatible endpoint |
Both are open source and work with any OpenAI-compatible endpoint — which is exactly what llama.cpp and Ollama expose.
Setup
Linux
# Pi — installer script (pins dependencies)
curl -fsSL https://pi.dev/install.sh | sh
# OpenCode — installer script
curl -fsSL https://opencode.ai/install | bashWindows
PowerShell for Pi:
powershell -c "irm https://pi.dev/install.ps1 | iex"For OpenCode, use WSL for the best experience:
# inside WSL — Windows files are reachable at /mnt/c/
curl -fsSL https://opencode.ai/install | bashNative Windows alternatives (OpenCode): Chocolatey, Scoop, or npm:
choco install opencode
# or
npm install -g opencode-ainpm fallbacks for both agents:
npm install -g --ignore-scripts @earendil-works/pi-coding-agent # Pi, needs Node ≥ 22.19
npm install -g opencode-ai # OpenCodeTerminal choice
Both agents are TUIs; use a modern terminal (Ghostty, WezTerm, Alacritty, Kitty) for the best rendering.
Provider config files
- Pi:
~/.pi/agent/models.json(or a project-levelmodels.json)- OpenCode:
~/.config/opencode/opencode.json(global) oropencode.json(per project)
Connect to llama.cpp
https://pi.dev/docs/latest/llama-cpp
llama serve --no-models-autoload -ngl all -c 32768
listening on http://127.0.0.1:9931
Pi — add to models.json:
{
"$schema": "https://pi.dev/schemas/models.schema.json",
"providers": {
"llamacpp": {
"baseUrl": "http://localhost:9931/v1",
"api": "openai-completions",
"apiKey": "llama",
"models": [
{ "id": "qwen3-4b-instruct" }
]
}
}
}OpenCode — add to opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"llama.cpp": {
"npm": "@ai-sdk/openai-compatible",
"name": "llama-server (local)",
"options": {
"baseURL": "http://localhost:8080/v1"
},
"models": {
"qwen3-4b-instruct": { "name": "Qwen3 4B (local)" }
}
}
}
}Install pi web-access
pi install npm:pi-agent-web-accessConnect to Ollama
ollama serve
ollama pull qwen2.5-coder:7bPi — models.json:
{
"$schema": "https://pi.dev/schemas/models.schema.json",
"providers": {
"ollama": {
"baseUrl": "http://localhost:11434/v1",
"api": "openai-completions",
"apiKey": "ollama",
"models": [
{ "id": "qwen2.5-coder:7b" }
]
}
}
}The dummy key is a placeholder; Ollama ignores it. For OpenAI-compatible servers that are stricter, set "compat": { "supportsDeveloperRole": false, "supportsReasoningEffort": false }.
OpenCode — two options:
# Option A — automatic: Ollama configures OpenCode for you
ollama launch opencode// Option B — manual, opencode.json
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama (local)",
"options": {
"baseURL": "http://localhost:11434/v1"
},
"models": {
"qwen2.5-coder:7b": { "name": "Qwen2.5 Coder 7B (local)" }
}
}
}
}Tool calls and context
If the agent’s file edits or shell commands fail, the model’s context window is usually too small. Raise
num_ctxin Ollama (16k–32k to start; OpenCode recommends 64k+).
Connect to OpenRouter
One API key for many cloud models. Get a key at https://openrouter.ai, then:
Pi — run /login in the TUI, choose OpenRouter, paste the key. Or set OPENROUTER_API_KEY and add the provider to models.json.
OpenCode — run /connect in the TUI, search for OpenRouter, paste the key. Or set OPENROUTER_API_KEY and pick a model in /models (e.g. openrouter/qwen/qwen3-coder).
Select the model with /model (Pi) or /models (OpenCode). OpenRouter is a good fallback when local models are slow on large refactors.
Guided usage
A typical session, step by step:
-
Start the server and the agent
ollama serve & # or llama serve cd ~/projects/myrepo opencode # or: pi -
Initialize the project — run
/init(OpenCode). It analyzes the project and writes anAGENTS.mddescribing structure and conventions. Commit it to Git so the agent keeps context. -
Ask a question — start read-only:
How is authentication handled in @src/api/index.ts?@fuzzy-searches files in the project. -
Plan first — in OpenCode, press
Tabto switch to Plan mode (indicator bottom-right). The agent proposes how to implement without touching files. In Pi, ask it to plan before editing. -
Execute — switch back to Build mode, describe the change, review the proposed diffs.
-
Review and continue — accept or reject edits; the session keeps history, so follow-ups like “now add tests” work.
Most used functionalities
| Function | Pi | OpenCode |
|---|---|---|
| Switch model | /model or Ctrl+L | /models |
| Cycle favorites | Ctrl+P | /models picker |
| Add provider | /login, models.json | /connect, opencode.json |
| Init project | SYSTEM.md per-project | /init → AGENTS.md |
| Reference files | plain paths / @ | @ fuzzy search |
| Plan mode | ask for a plan | Tab toggles Plan/Build |
| Run commands | built-in tools | built-in tools |
| Web fetch | yes (web search / fetch) | yes (web fetch) |
| Sessions | ~/.pi/agent/ history | /sessions, shareable links |
| Extensibility | extensions, skills, packages | plugins, commands, config |
Agents can run shell commands
In Build mode the agent executes commands in your project. On production systems or shared hosts, review each proposed command before approving — especially
rm,git push,systemctl, or anything that touches data outside the project.
Example 1 — Python coding
Goal: refactor a small script into a package with tests.
# 1. Open the agent in the project
cd ~/projects/data-tools
opencode # or: pi
# 2. Plan mode (Tab in OpenCode)
Add a `--format json` option to tools/csvsum.py.
Plan the changes first: function signature, CLI wiring, and output shape.
# 3. Build mode
Implement it. Update the README usage section.
# 4. Ask for tests
Add pytest tests for the json output path, covering proa malformed CSV.
# 5. Verify
Run the tests and show me the summary.The agent edits tools/csvsum.py, writes tests/test_csvsum.py, runs pytest, and reports the diff — all with the local model, no MCP or RAG needed.
Example 2 — Managing files and solving local problems
Goal: clean up downloads and diagnose a failing script.
# 1. Open the agent in a folder, not a repo
cd ~/Downloads
opencode
# 2. Ask for a plan before touching anything
List all CSV files here, group them by month in their filename,
and propose a plan to move them into monthly folders.
# 3. Approve and execute
Do it. Print the mv commands you run.
# 4. Diagnose a local problem
python scripts/backup.py fails with "PermissionError".
Explain the traceback and fix the script.The agent can read the traceback, open the file, edit it, and re-run the command. A local model suffices for this class of task — the context is small and self-contained.
Example 3 — Local documentation
Goal: generate documentation from your own notes.
# 1. Open the agent in the vault folder
cd ~/Documents/Obsidian
opencode
# 2. Ask for an index
@RUMOS/DOCS/llm/ — list the notes in this folder and
summarize each in one sentence.
# 3. Generate a guide
Create a new note "LM-index.md" that links all the notes
in the folder with a one-line summary each,
using Obsidian [[wikilinks]].
# 4. Keep it consistent
Check the frontmatter of the notes: missing dates or authors,
report them in a table.The agent reads sibling notes, produces a linked index, and audits frontmatter — useful for documentation work without any RAG plumbing, since the agent can read files directly.
Run — quick checklist
# 1. Serve a model
ollama serve && ollama pull qwen2.5-coder:7b
# 2. Install an agent
curl -fsSL https://opencode.ai/install | bash # or pi.dev/install.sh
# 3. Configure the provider (opencode.json / models.json)
# 4. Init and run
cd ~/projects/myrepo && opencode
/init