Extend and serve long context windows safely with YaRN and RoPE scaling, context-length flags, chunked prefill, sliding window attention, KV-cache memory math and context parallelism, with per-engine flags for vLLM, SGLang, TensorRT-LLM, llama.cpp, TensorFold, Ollama and ExLlamaV3. Use for --max-model-len, --context-length, --hf-overrides rope_parameters, --rope-scaling yarn, num_ctx, OLLAMA_CONTEXT_LENGTH, --chunked-prefill-size, -dcp, out of memory at long prompts, "prompt too long" errors, or quality loss after context extension.
Raise the usable context window of a served model in the right order: fit the KV cache, set the engine's context flag, extend positions only when the model needs it, and prove with long-context tests that quality held.
Scope: this cross-product technique, with per-engine flags and quality gates. Product install and tuning stay in product skills.
Work through the steps in order. Load a reference only when a step names it.
Record: native context (max_position_embeddings and any existing rope_scaling), KV heads and head dim, layers, KV dtype, free memory, the context the user needs (prompt plus reply), and the number of concurrent requests.
Decision flow
Is the needed context at or below the model's native window? Yes: set the engine's context flag to the need, do not extend RoPE. Go to the KV memory math.
Does the model card already ship a long variant or document a YaRN setting? Use the vendor's exact numbers. Some models (for example Qwen3) are validated to 4x with static YaRN. Do not invent a factor.
Needed context above native and no long variant: RoPE scaling (YaRN preferred) with factor set to the smallest value that covers the need (need divided by native, rounded up). This changes numerics on every request. Gate it (see Rules).
KV cache does not fit? Work down this list, one change at a time: lower concurrency, lower the context, quantize the KV cache (kv-cache-quantization skill), offload KV (kv-cache-offloading skill), add GPUs with TP or context parallel (-dcp in vLLM), then a smaller or sliding-window model.
Time to first token on long prompts is too slow or blocks decode? Tune chunked prefill, or add prefill context parallel where the engine supports it. See references/engine-matrix.md.
Rules
Ask before changing the system. Config edits, restarts and memory settings need approval and an undo path.
Secrets never touch output. Keys live in 0600 files or environment variables.
Verify upstream. Flags move between releases. vLLM removed --rope-scaling for --hf-overrides with rope_parameters. Check --help of the installed version.
Numerics gate. RoPE or YaRN scaling, a quantized KV cache and sliding-window overrides change outputs. Static YaRN applies the scaling to short prompts too, so short-text quality can drop (Qwen's model card says so). Gate every such change with the llm-evals skill (install: npx skills add ScaleDockHQ/llm-skills --skill llm-evals) using references/quality-gate.md: needle-in-a-haystack and RULER-style checks at several lengths, a short-prompt regression check, and a rollback.
A bigger window is not better quality. Retrieval accuracy usually drops with length. Measure at the lengths you will really use.
Memory first. The flag that raises the window also reserves KV memory for it. Check the startup log for the KV capacity before announcing success.
KV memory math
Per token, per sequence: 2 x layers x kv_heads x head_dim x bytes_per_element. Multiply by tokens, then by concurrent sequences. This is standard arithmetic for full-attention layers; read the exact values from config.json.
Example: 48 layers, 4 KV heads, head dim 128, FP16 (2 bytes): 2 x 48 x 4 x 128 x 2 = 98,304 bytes per token, so 131,072 tokens is 12 GiB for one sequence. These are the Qwen3-30B-A3B layer and KV-head counts from its model card; head dim 128 is assumed here (check config.json). Models with sliding-window layers, MLA or linear-attention layers need less, so trust the engine's startup log over this formula. With tensor parallel the cache is split across GPUs, but KV heads fewer than TP ranks get replicated (TensorRT-LLM and vLLM docs say so).
Techniques per engine
Baseline: serve the current config, record KV capacity from the log, run the llm-evals baseline plus a short needle test (references/quality-gate.md).
Set the context flag from references/engine-matrix.md to the real need, not the model maximum.
Only if above native: add RoPE/YaRN scaling with the smallest factor, using the engine's exact key (vLLM --hf-overrides, SGLang --json-model-override-args, llama.cpp --rope-scaling yarn --rope-scale N --yarn-orig-ctx N).
Tune chunked prefill and batch tokens for time to first token while decode runs.
Add context parallel only for a measured memory or latency problem (vLLM -dcp, SGLang --attn-cp-size or --dcp-size, TensorRT-LLM cp_size).
Verify: the endpoint accepts a prompt near the limit, KV capacity in the log covers the target concurrency, long-context checks pass at 25%, 50%, 75% and 100% of the new window, and short-prompt scores did not drop.
Verify
curl -fsS http://127.0.0.1:8000/v1/models | jq '.data[] | {id, max_model_len, context_length}'
# Send a prompt of about 90% of the window (generate filler with seq or a fixture file), ask a fact planted in the middle
curl -fsS http://127.0.0.1:8000/v1/chat/completions -H 'Content-Type: application/json' \
-d @needle-request.json | jq -r '.choices[0].message.content, .usage'
Then run references/quality-gate.md. Keep the previous launch command as the rollback.
Reference index
references/engine-matrix.md: per-engine context, RoPE/YaRN, chunked prefill, sliding window and context-parallel flags with defaults and constraints, and the TensorFold, Ollama and ExLlamaV3 status. Load at the "set the flag" step.
references/quality-gate.md: baseline-versus-candidate procedure with needle and RULER-style long-context checks, short-prompt regression check and rollback. Load before any RoPE, KV-dtype or window change.
Companion skills
Install any with npx skills add ScaleDockHQ/llm-skills --skill <name>. Engine skills may not be published yet; use upstream docs until they are.
vllm, sglang, tensorrt-llm, llama-cpp, ollama, tensorfold, exllamav3: install, serve and tune each engine.
llm-evals: the quality gate for any numerics-changing option.
kv-cache-quantization, kv-cache-offloading, prefix-aware-routing: stretch memory and reuse prompts.
multi-node-inference, disaggregated-serving: spread long prefill across nodes.
choosing-hardware, choosing-inference-engine: pick GPUs and engine for a long-context workload.
moe-serving, multi-lora-serving, speculative-decoding, reasoning-models: techniques that interact with context memory.
1---2name: long-context3description: Extend and serve long context windows safely with YaRN and RoPE scaling, context-length flags, chunked prefill, sliding window attention, KV-cache memory math and context parallelism, with per-engine flags for vLLM, SGLang, TensorRT-LLM, llama.cpp, TensorFold, Ollama and ExLlamaV3. Use for --max-model-len, --context-length, --hf-overrides rope_parameters, --rope-scaling yarn, num_ctx, OLLAMA_CONTEXT_LENGTH, --chunked-prefill-size, -dcp, out of memory at long prompts, "prompt too long" errors, or quality loss after context extension.4license: MIT5compatibility: Linux with NVIDIA GPUs for vLLM, SGLang and TensorRT-LLM; llama.cpp, Ollama and TensorFold also run on Apple Silicon. Needs curl and jq. Config changes and restarts need the user's approval.6metadata:7 source: https://skillmd.com/skills/scaledockhq/long-context8 author: ScaleDockHQ9 version: "0.1.0"10---1112# Long Context1314Raise the usable context window of a served model in the right order: fit the KV cache, set the engine's context flag, extend positions only when the model needs it, and prove with long-context tests that quality held.1516**Scope:** this cross-product technique, with per-engine flags and quality gates. Product install and tuning stay in product skills.1718Work through the steps in order. Load a reference only when a step names it.1920## Discover first2122Read-only. Never print secrets or tokens.2324```bash25jq '{max_pos: .max_position_embeddings, rope: (.rope_scaling // .rope_parameters), layers: .num_hidden_layers, heads: .num_attention_heads, kv_heads: .num_key_value_heads, head_dim: .head_dim, window: .sliding_window, types: .layer_types}' /path/to/model/config.json26nvidia-smi --query-gpu=index,name,memory.total,memory.used --format=csv27command -v vllm sglang trtllm-serve llama-server ollama tensorfold-native28curl -fsS http://127.0.0.1:8000/v1/models | jq '.data[] | {id, max_model_len, context_length}' # vLLM and TensorFold advertise the window29ollama ps 2>/dev/null # CONTEXT column shows the allocated window30```3132Record: native context (`max_position_embeddings` and any existing `rope_scaling`), KV heads and head dim, layers, KV dtype, free memory, the context the user needs (prompt plus reply), and the number of concurrent requests.3334## Decision flow35361. **Is the needed context at or below the model's native window?** Yes: set the engine's context flag to the need, do not extend RoPE. Go to the KV memory math.372. **Does the model card already ship a long variant or document a YaRN setting?** Use the vendor's exact numbers. Some models (for example Qwen3) are validated to 4x with static YaRN. Do not invent a factor.383. **Needed context above native and no long variant:** RoPE scaling (YaRN preferred) with `factor` set to the smallest value that covers the need (need divided by native, rounded up). This changes numerics on every request. Gate it (see Rules).394. **KV cache does not fit?** Work down this list, one change at a time: lower concurrency, lower the context, quantize the KV cache (`kv-cache-quantization` skill), offload KV (`kv-cache-offloading` skill), add GPUs with TP or context parallel (`-dcp` in vLLM), then a smaller or sliding-window model.405. **Time to first token on long prompts is too slow or blocks decode?** Tune chunked prefill, or add prefill context parallel where the engine supports it. See `references/engine-matrix.md`.4142## Rules4344- **Ask before changing the system.** Config edits, restarts and memory settings need approval and an undo path.45- **Secrets never touch output.** Keys live in `0600` files or environment variables.46- **Verify upstream.** Flags move between releases. vLLM removed `--rope-scaling` for `--hf-overrides` with `rope_parameters`. Check `--help` of the installed version.47- **Numerics gate.** RoPE or YaRN scaling, a quantized KV cache and sliding-window overrides change outputs. Static YaRN applies the scaling to short prompts too, so short-text quality can drop (Qwen's model card says so). Gate every such change with the `llm-evals` skill (install: `npx skills add ScaleDockHQ/llm-skills --skill llm-evals`) using `references/quality-gate.md`: needle-in-a-haystack and RULER-style checks at several lengths, a short-prompt regression check, and a rollback.48- **A bigger window is not better quality.** Retrieval accuracy usually drops with length. Measure at the lengths you will really use.49- **Memory first.** The flag that raises the window also reserves KV memory for it. Check the startup log for the KV capacity before announcing success.5051## KV memory math5253Per token, per sequence: `2 x layers x kv_heads x head_dim x bytes_per_element`. Multiply by tokens, then by concurrent sequences. This is standard arithmetic for full-attention layers; read the exact values from `config.json`.5455Example: 48 layers, 4 KV heads, head dim 128, FP16 (2 bytes): `2 x 48 x 4 x 128 x 2 = 98,304` bytes per token, so 131,072 tokens is 12 GiB for one sequence. These are the Qwen3-30B-A3B layer and KV-head counts from its model card; head dim 128 is assumed here (check `config.json`). Models with sliding-window layers, MLA or linear-attention layers need less, so trust the engine's startup log over this formula. With tensor parallel the cache is split across GPUs, but KV heads fewer than TP ranks get replicated (TensorRT-LLM and vLLM docs say so).5657## Techniques per engine5859- [ ] Baseline: serve the current config, record KV capacity from the log, run the `llm-evals` baseline plus a short needle test (`references/quality-gate.md`).60- [ ] Set the context flag from `references/engine-matrix.md` to the real need, not the model maximum.61- [ ] Only if above native: add RoPE/YaRN scaling with the smallest factor, using the engine's exact key (vLLM `--hf-overrides`, SGLang `--json-model-override-args`, llama.cpp `--rope-scaling yarn --rope-scale N --yarn-orig-ctx N`).62- [ ] Tune chunked prefill and batch tokens for time to first token while decode runs.63- [ ] Add context parallel only for a measured memory or latency problem (vLLM `-dcp`, SGLang `--attn-cp-size` or `--dcp-size`, TensorRT-LLM `cp_size`).64- [ ] Verify: the endpoint accepts a prompt near the limit, KV capacity in the log covers the target concurrency, long-context checks pass at 25%, 50%, 75% and 100% of the new window, and short-prompt scores did not drop.6566## Verify6768```bash69curl -fsS http://127.0.0.1:8000/v1/models | jq '.data[] | {id, max_model_len, context_length}'70# Send a prompt of about 90% of the window (generate filler with seq or a fixture file), ask a fact planted in the middle71curl -fsS http://127.0.0.1:8000/v1/chat/completions -H 'Content-Type: application/json' \72 -d @needle-request.json | jq -r '.choices[0].message.content, .usage'73```7475Then run `references/quality-gate.md`. Keep the previous launch command as the rollback.7677## Reference index7879- `references/engine-matrix.md`: per-engine context, RoPE/YaRN, chunked prefill, sliding window and context-parallel flags with defaults and constraints, and the TensorFold, Ollama and ExLlamaV3 status. Load at the "set the flag" step.80- `references/quality-gate.md`: baseline-versus-candidate procedure with needle and RULER-style long-context checks, short-prompt regression check and rollback. Load before any RoPE, KV-dtype or window change.8182## Companion skills8384Install any with `npx skills add ScaleDockHQ/llm-skills --skill <name>`. Engine skills may not be published yet; use upstream docs until they are.8586- `vllm`, `sglang`, `tensorrt-llm`, `llama-cpp`, `ollama`, `tensorfold`, `exllamav3`: install, serve and tune each engine.87- `llm-evals`: the quality gate for any numerics-changing option.88- `kv-cache-quantization`, `kv-cache-offloading`, `prefix-aware-routing`: stretch memory and reuse prompts.89- `multi-node-inference`, `disaggregated-serving`: spread long prefill across nodes.90- `choosing-hardware`, `choosing-inference-engine`: pick GPUs and engine for a long-context workload.91- `moe-serving`, `multi-lora-serving`, `speculative-decoding`, `reasoning-models`: techniques that interact with context memory.
Run npx get-skill add scaledockhq/long-context in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Extend and serve long context windows safely with YaRN and RoPE scaling, context-length flags, chunked prefill, sliding window attention, KV-cache memory math and context parallelism, with per-engine flags for vLLM, SGLang, TensorRT-LLM, llama.cpp, TensorFold, Ollama and ExLlamaV3. Use for --max-model-len, --context-length, --hf-overrides rope_parameters, --rope-scaling yarn, num_ctx, OLLAMA_CONTEXT_LENGTH, --chunked-prefill-size, -dcp, out of memory at long prompts, "prompt too long" errors, or quality loss after context extension. It is listed under AI & ML on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Capability flags: makes network calls, reads secrets. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free. This skill is licensed under MIT.
ScaleDockHQ (@scaledockhq) published this skill. Their other Agent Skills are listed on their SkillMD profile.