llama.cpp Server Mode: Your Local OpenAI-Compatible API

llama.cpp's server mode exposes a local REST API that mimics OpenAI's chat completions endpoint. Any application that works with the OpenAI API can point to your local server instead, getting free, private inference. It's one of the most powerful and underused features in the local AI ecosystem.

Drop-in replacement: Change base_url="https://api.openai.com/v1" to base_url="http://localhost:8080/v1" in any OpenAI SDK call. Hardware recommendations for running a personal API server at LLMHardware.io.

Starting the Server

Run: ./llama-server -m model.gguf --port 8080 --host 0.0.0.0 -n 4096 --n-gpu-layers 99. The --host 0.0.0.0 flag makes it accessible from other devices on your network. Use --api-key yourkey to add a bearer token for security. The server exposes /v1/chat/completions, /v1/completions, and /v1/embeddings.

Useful Server Flags

--parallel 4 enables up to 4 concurrent requests with shared KV cache. --cont-batching enables continuous batching for better throughput. --flash-attn speeds up long contexts. --cache-type-k q8_0 and --cache-type-v q8_0 quantize the KV cache, reducing memory usage by ~50% for long conversations.

Integrating with Existing Tools

Continue (VS Code AI assistant), Cursor, and Zed all support custom OpenAI-compatible endpoints. Point them to your local server for free AI coding assistance. LangChain, LlamaIndex, and Semantic Kernel all support custom base URLs. The llama.cpp server also exposes a web UI at the root URL for testing without any additional tools. Combine with a systemd service file for an always-on local AI API.

Comments

Popular posts from this blog

AMD RX 9070 XT for Local LLMs: ROCm Performance Guide (2026)

llama.cpp flash attention: how to enable and benchmark

SillyTavern with Local LLMs: Complete Setup for AI Companions