Self-Hosting Open-Source Coding LLMs: How to Choose, Size, and Deploy
A practical guide to self-hosting open-source coding models: how to decide whether self-hosting is worth it, read model cards, estimate VRAM for weights and KV cache, benchmark the metrics that matter, launch vLLM on single and multi-node setups, and troubleshoot common production issues.
Self-Hosting Open-Source Coding LLMs: How to Choose, Size, and Deploy
1. First, Decide Whether Self-Hosting Is Worth It
Self-hosting a coding model usually makes sense under two conditions: your code or business data cannot leave your internal network, or your call volume is high enough that API costs exceed the cost of your own hardware. If neither applies, using a cloud API is simply easier. Your team will also need at least one ops engineer who is comfortable with GPU drivers, containers, and inference serving.
2. Read the Model Card First
Before choosing a model, open the official model card and config.json of each candidate and note the following:
- Total and activated parameter counts. Total parameters determine the floor for weight storage and VRAM. MoE (Mixture of Experts) models activate only a subset of experts per token, so compute is driven by activated parameters, but VRAM must still hold the weights of all experts.
- Context window. The model card reports the maximum length, which is not the same as the recommended working length.
- Attention architecture. The number of layers, KV heads, and head dimension determine KV cache size. Sparse or compressed attention designs change this calculation—defer to the official documentation.
- License. Permissive licenses generally allow commercial use, modification, and redistribution, but read the specific terms, especially usage restrictions and requirements for publishing derivative models.
- Weight format. BF16, FP8, GPTQ, AWQ, and other formats have different VRAM footprints, and vLLM launch parameters must match the format.
3. How to Estimate VRAM
Weight VRAM equals parameter count × bytes per parameter. BF16 uses 2 bytes, FP8 uses 1 byte, and INT4 uses 0.5 bytes. For example, a 200B-parameter model needs roughly 200 GB for FP8 weights or 400 GB for BF16. These numbers are for illustration only—always use the actual figures for your chosen model.
KV cache VRAM grows with context length and concurrency, estimated as:
KV cache bytes ≈ 2 × layers × KV heads × head dim × bytes per element × sequence length × concurrent requests
Assume a configuration of 60 layers, 8 KV heads, head dimension 128, stored in BF16. The KV cache per token is about 2 × 60 × 8 × 128 × 2 = 245,760 bytes, roughly 240 KB.
- A single 262,144-token (256K) request needs about 64 GB.
- A single 1,048,576-token (1M) request needs about 258 GB.
- Four concurrent 256K requests need about 258 GB.
The takeaway: fitting the weights in VRAM does not mean you can support 1M contexts or high concurrency. This formula applies to standard attention architectures; compressed or sparse designs will differ. The most reliable number is the KV cache capacity reported in the vLLM startup log, which reflects the real ceiling on your current hardware and settings.
4. Four Metrics to Measure Before Choosing
Measure these on your own tasks—do not rely solely on public leaderboards.
- Task pass rate. Sample 50 to 200 real tasks from your history, such as fixing bugs, adding unit tests, or refactoring functions. Count the share of outputs that pass tests and code review. Fix the temperature, prompt template, and evaluation script version so every comparison is consistent.
- Cost per successful task. Compute the average cost per passing task. For self-hosting: (GPU cost per hour × runtime hours + ops labor) ÷ passing tasks. For API usage: (input/output tokens × unit price + retries) ÷ passing tasks.
- Latency and throughput. At your target concurrency, measure P95 time-to-first-token (TTFT) and output tokens per second. The vLLM repository ships benchmark scripts you can use directly for load testing.
- Effective long-context utilization. Prepare files of varying lengths (32K, 128K, 256K, etc.) and embed a function signature or config value at the beginning, middle, and end of each. Check whether the model can reference them accurately, and record accuracy per length and position. If accuracy drops noticeably, reduce the context length you actually use.
5. Launching the Service
Replace the model paths, service names, and version numbers in the following commands with your actual values. For vLLM, CUDA, and driver versions, use the compatible combinations specified in the official documentation.
Verify weights before starting. Download the checksum file from the official release page and run:
sha256sum -c SHA256SUMS
Scenario 1: Single node with 8 GPUs. For example, 8 × 141 GB, about 1,128 GB of VRAM total. A single node only needs tensor parallelism—no pipeline parallelism required.
pip install "vllm==<version specified in official docs>"vllm serve /models/candidate-model \--served-model-name coder \--tensor-parallel-size 8 \--max-model-len 262144 \--gpu-memory-utilization 0.90 \--api-key "$VLLM_API_KEY" \--port 8000
Scenario 2: 2 nodes × 8 GPUs = 16 GPUs. Cross-node setups require Ray to form the cluster. Keep tensor parallelism within a single node; pipeline parallelism handles the cross-node split.
# Node A: start the Ray head noderay start --head --port=6379# Node B: join the cluster, replacing <node-A-IP> with the actual addressray start --address=<node-A-IP>:6379# Run on Node A: TP 8 × PP 2 = 16 GPUsvllm serve /models/candidate-model \--served-model-name coder \--tensor-parallel-size 8 \--pipeline-parallel-size 2 \--max-model-len 262144 \--api-key "$VLLM_API_KEY" \--port 8000
Parameter notes:
--served-model-namesets themodelfield clients must send in requests. Clients must use this exact name, otherwise the server returns 404.- Set
--max-model-lento 262144 for initial validation, then increase gradually once stable. - If the weights are already in the official FP8 format, the configuration is usually detected automatically—do not add
--quantizationmanually. For AWQ, GPTQ, and similar formats, set the corresponding parameter per the official documentation. - Only add
--trust-remote-codeif the model card explicitly requires it, and review the code in the model repository first. - By default, the service should listen only on an internal network address, with authentication enabled via
--api-key.
6. Calling the Service from a Client
import osfrom openai import OpenAIclient = OpenAI(base_url="http://10.0.0.10:8000/v1", # replace with the server's internal addressapi_key=os.environ["VLLM_API_KEY"],)resp = client.chat.completions.create(model="coder", # must match --served-model-namemessages=[{"role": "user", "content": "Refactor the following function and add unit tests: ..."}],temperature=0.2,max_tokens=2048,)print(resp.choices[0].message.content)
For code generation and modification tasks, a temperature of 0.0 to 0.3 usually gives more stable results. Raise it moderately when you need multiple candidate solutions. Always set a max_tokens limit so a single request cannot hold VRAM for an extended period.
7. Troubleshooting Common Issues
- Out of VRAM at startup. Check the KV cache capacity in the logs, then reduce
--max-model-len,--gpu-memory-utilization, or the concurrency limit, in that order. - 404 errors saying the model does not exist. Verify that the client’s
modelfield exactly matches--served-model-name. - “Unrecognized model architecture” at startup. Usually means the vLLM version is too old—upgrade to the version specified in the official documentation.
- Quantization errors while loading weights. Check whether you added a
--quantizationparameter that does not match the weight format; remove it or set the correct value. - High time-to-first-token. Prefilling long prompts is inherently slow on the first request. Enabling prefix caching (
--enable-prefix-caching) reuses identical prefixes and helps subsequent requests with the same repository context or system prompt. It will not reduce the latency of the first request. - Low throughput. Check cross-node network bandwidth and prefer high-speed interconnects such as InfiniBand. Also confirm that tensor parallelism is not spanning nodes.
8. Production Checklist
- Rate limiting. Limit by token count rather than request count, and use a separate queue for long-context requests so a few requests cannot monopolize VRAM.
- Monitoring.
