Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pi Agent and llama.cpp Local Models

Pi Agent and llama.cpp Local Models

Pi Agent has built-in support for llama.cpp, letting you run open-source large models locally without a cloud API.


What is llama.cpp

llama.cpp is a high-performance LLM inference engine that can run open-source large language models on consumer hardware.

It supports CPU inference (greatly reducing memory requirements via quantization) and GPU acceleration (CUDA, Metal, Vulkan, etc.).

Pi Agent communicates with local models through the llama.cpp router server.

Schematic of the link between Pi Agent, the llama.cpp router, and local model files, plus an optional cloud path


Configure llama.cpp

Configuration has three steps: first start the llama-server router, then register the llama.cpp provider, and finally load and select a model.

Start the llama-server router

Starting llama-server without the -m or --model parameter enters router mode.

Once a model parameter is passed, llama-server enters single-model mode and no longer scans the model directory.

The router loads or unloads GGUF models in the --models-dir directory on demand. Common parameters include --models-dir (model directory), --no-models-autoload (disable autoload), --jinja (enable chat templates and tool calling support), -ngl (number of layers offloaded to GPU), and -c 32768 (context window per model).



llama-server \

  --models-dir ~/models \

  --no-models-autoload \

  --jinja \

  --host 127.0.0.1 \

  --port 8080 \

  -ngl 999 \

  -c 32768

After startup, you can use the /health and /models endpoints to check whether the router is reachable and whether models are discovered.

Log in to llama.cpp

In Pi Agent interactive mode:



/login llama.cpp

This step only registers the local provider; no account authentication is needed, but you must fill in the router address (default http://127.0.0.1:8080).

After execution, Pi will ask for the router URL and an optional API Key; for local services you can usually leave the API Key blank.

Manage local models

Use the /llama command to open the model management menu:



/llama

Select an unloaded model in the menu and press Enter to load it into memory.

Select a loaded model and press Enter to unload it.

Selecting the menu item Download model... lets you search Hugging Face and download new models; during loading or downloading, press Escape to confirm cancellation.

The menu always shows the router's current real state, and only loaded models appear in the /model picker.

Switch to a local model

Use the /model command to select a loaded local model.

As for keybindings, Ctrl+L opens the model picker and Ctrl+P cycles through models.


Recommended open-source models

Here are some open-source coding models suitable for local running:

Model Number of parameters Minimum VRAM/RAM Applicable scenarios
Qwen 2.5 Coder 7B / 14B / 32B 8G / 16G / 32G General coding, multilingual
DeepSeek Coder V2 16B / 236B 16G / 128G+ Complex programming tasks
CodeLlama 7B / 13B / 34B 8G / 16G / 32G Code completion, general coding
Mistral 7B 8G Lightweight coding assistant

Local models are usually weaker at coding than top cloud models (such as Claude Sonnet, GPT-4o), but are a great alternative when the network is restricted, data sensitivity is high, or frequent use is needed.


Model storage

The model directory is determined by the llama-server startup parameter --models-dir (e.g. ~/models), and model downloads are also performed by the llama.cpp server.

Model files are usually large (a few GB to tens of GB), so make sure this directory has enough disk space.


Custom llama.cpp server

If your llama.cpp server runs on a non-default port or a remote machine, there are two ways to connect.

The no-code approach is to set the environment variables LLAMA_BASE_URL and LLAMA_API_KEY to point to the custom llama.cpp server; no extension needed and no /login either.



export LLAMA_BASE_URL=http://192.168.1.20:8080

export LLAMA_API_KEY=optional-secret

pi

If the server has an API Key enabled, start llama-server with the same --api-key value and keep --host 127.0.0.1 to restrict access to the local machine.

When you only need to connect to an OpenAI-compatible API, the models.json approach is simpler; see the "Connecting to DeepSeek" chapter; use the extension approach only when you need full control over the request flow.

Register the llama.cpp provider in an extension:

Example

// File path: ~/.pi/agent/extensions/llamacpp-provider.ts

pi.

registerProvider

(

"llama.cpp"

,

{

  name

:

"Local llama.cpp"

,

  baseUrl

:

"http://localhost:8080/v1"

,

  apiKey

:

"local"

,

  api

:

"openai-completions"

,

 

// Dynamically discover loaded models

  async refreshModels

(

{

signal

}

)

{

   

const

response

=

await fetch

(

     

"http://localhost:8080/v1/models"

,

     

{

signal

}

   

)

;

   

const

{

data

}

=

await response.

json

(

)

;

   

return

data.

map

(

(

{

id

}

)

=>

(

{

      id

,

      name

:

id

,

      reasoning

:

false

,

      input

:

[

"text"

]

,

      cost

:

{

        input

:

0

,

        output

:

0

,

        cacheRead

:

0

,

        cacheWrite

:

0

,

     

}

,

      contextWindow

:

128000

,

      maxTokens

:

16384

,

   

}

)

)

;

 

}

,

}

)

;

This dynamic discovery mode lets Pi Agent get the list of models currently loaded on the llama.cpp server in real time.

Save the file to the ~/.pi/agent/extensions/ directory and restart Pi Agent; the extension will load automatically and register this provider.

Once active, open the /model picker to see the models currently loaded on the llama.cpp server and chat directly.


Limitations of local models

Note the following when using local models:

When tool calling is weak, the most common symptom is the model outputting tool calls as text rather than actually triggering the tool.



> 查看 src/index.ts 的内容



好的,下面是 src/index.ts 的内容:

import { createAgent } from "pi";

export const agent = createAgent();

...

No tool call card appearing in the message area means the model didn't actually call the read tool but made up the file content itself.

In this case, switch to a model with better tool calling support or break the task into smaller steps.

Recommended hybrid strategy: use cloud models (such as Claude Sonnet) when deep reasoning and complex coding are needed, and switch to local models for everyday simple tasks or when the network is restricted.

Use Ctrl+P to quickly switch between the two models.