OpenCode V2 plugin for enhanced llama.cpp support with auto-detection, dynamic model discovery, and enriched model metadata.
Note: The migration to the OpenCode V2 API (and related development work) was carried out with the assistance of AI tools. Code correctness and behavior should be reviewed before production use.
- Auto-detection: Automatically detects a llama.cpp server running on common ports (1234, 8080, 11434)
- Dynamic Model Discovery: Queries llama.cpp's
/v1/modelsendpoint and registers discovered models into the OpenCode catalog (V2 API) - Enriched Model Metadata:
- Human-readable display names, preferring the name provided by the API (e.g., "Qwen3 30B A3B" instead of "qwen/qwen3-30b-a3b")
- Context limit from
context_length/context_window - Output limit from llama-swap metadata when available
- Release date from the model's
createdfield - Tool support detection (
function_calling/toolsparameter) - Input/output modalities, with embedding models classified accordingly
- Organization Owner Extraction: Extracts and sets the
organizationOwnerfield from model IDs - Model Validation: A session context hook verifies that the selected model is actually loaded on the server (with retry/backoff) and adds a system warning to the prompt if it is not
- Periodic Refresh: Re-discovers models every 60 seconds and reloads the catalog
- Health Check Monitoring: Verifies the llama.cpp server is accessible before attempting operations
- Caching: A single shared model status cache reduces redundant API calls
- Error Handling: Smart error categorization with actionable messages
- Logging: Centralized logger with an
[opencode-llama-cpp]prefix; setDEBUG=1for verbose debug output
pnpm add opencode-plugin-llama.cppAdd the plugin to your opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugin": [
"opencode-plugin-llama.cpp"
],
"providers": {
"llama.cpp": {
"npm": "@ai-sdk/openai-compatible",
"name": "llama.cpp (local)",
"settings": {
"baseURL": "http://127.0.0.1:1234"
}
}
}
}Both configuration formats are supported:
- V1:
providerwithoptions.baseURL - V2:
providerswithsettings.baseURL
baseURL may include or omit the /v1 suffix — it is normalized automatically.
If you don't configure the llama.cpp provider, the plugin will automatically detect a llama.cpp server on one of the common ports and register the provider configuration for you.
You can also manually configure the provider with specific models:
{
"$schema": "https://opencode.ai/config.json",
"plugin": [
"opencode-plugin-llama.cpp"
],
"providers": {
"llama.cpp": {
"npm": "@ai-sdk/openai-compatible",
"name": "llama.cpp (local)",
"settings": {
"baseURL": "http://127.0.0.1:1234"
}
}
}
}The plugin will automatically discover and register any additional models available on the llama.cpp server that aren't already configured.
- On OpenCode startup, the plugin's
setupfunction runs and resolves the llama.cpp base URL:- It reads
opencode.json/opencode.jsoncfiles (global and project-level), supporting both V1 (provider.options.baseURL) and V2 (providers.settings.baseURL) formats - If not found, it falls back to the OpenCode provider catalog
- If still not found, it auto-detects a llama.cpp server on localhost ports 1234, 8080, and 11434
- It reads
- If a server is found, its health is checked via the
/v1/modelsendpoint - Discovered models are registered into the
llama.cppprovider in the OpenCode catalog, enriched with metadata (display name, limits, capabilities, modalities, organization owner) - Models are re-discovered every 60 seconds and the catalog is reloaded
- On every chat request, a session context hook validates that the selected model is loaded on the server (with retry/backoff); if validation fails, a warning is added to the system prompt
Set the DEBUG environment variable to enable verbose logging:
DEBUG=1 opencodeLog lines are prefixed with [opencode-llama-cpp].
- OpenCode (V2)
- llama.cpp server running locally (default port: 1234)
- Node.js 18+ (global
fetch)
The project uses pnpm exclusively.
pnpm install # install dependencies
pnpm test # run unit tests (157 tests, ~68% coverage)
pnpm test:coverage # run tests with coverage report
pnpm validate # lint + typecheck + testsMIT
Contributions are welcome! Please feel free to submit a Pull Request.