Small Language Model Design › Worked Example: Choosing a Configuration for a Deployed Assistant

Michael BrenndoerferDecember 1, 20265 min read
Language AI HandbookAll chapters
Part 43 of 67Small and On-Device Language Models
Chapter 327Small Language Model Design › Worked Example: Choosing a Configuration for a Deployed Assistant
Reading section 8 of 11 · Worked Example: Choosing a Configuration for a Deployed Assistant · 5 minSection 8 of 11 · 5 min

Section 8 of 11 · 5 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Suppose you are building an English customer-support assistant for a single product. It needs to run on a laptop with 4 GB available to the process, retrieve from a few thousand manual passages, and produce a JSON reply with response and followup_question fields. For an interactive chat interface, you will measure time to the first token as well as time to the full response.

We use three illustrative candidates, each with one 4,096-token sequence, fp16 KV cache, and ideally packed 4-bit weights. The example reserves 0.6 GB for other costs such as quantization metadata, activations, tokenizer, and runtime. This fixed reserve is an assumption. An actual format and runtime need their own memory measurements. The candidates' parameter counts and head settings are independent fixtures for deployment, rather than rounded aliases for the earlier architecture examples.

PythonIn [10]
CollapseExpand
candidates = [
    dict(
        name="0.5B q4",
        params=500_000_000,
        n_layers=24,
        n_kv_heads=8,
        d_head=128,
    ),
    dict(
        name="3B q4",
        params=3_000_000_000,
        n_layers=26,
        n_kv_heads=8,
        d_head=128,
    ),
    dict(
        name="7B q4",
        params=7_000_000_000,
        n_layers=32,
        n_kv_heads=8,
        d_head=128,
    ),
]
deployment_context_tokens = 4096
device_budget_gb = 4.0


def deployed_memory_gb(
    params,
    n_layers,
    n_kv_heads,
    d_head,
    context=deployment_context_tokens,
    bits=4,
    overhead=0.6,
):
    weights_gb = params * bits / 8 / 1e9
    kv_gb = kv_cache_bytes(context, n_layers, n_kv_heads, d_head) / 1e9
    return {
        "weights": weights_gb,
        "kv_cache": kv_gb,
        "overhead": overhead,
        "total": weights_gb + kv_gb + overhead,
    }


budgets = {
    candidate["name"]: deployed_memory_gb(
        **{key: value for key, value in candidate.items() if key != "name"}
    )
    for candidate in candidates
}
ConsoleOut [11]
CollapseExpand
candidate    weights   KV cache   reserve    total   fits?
0.5B q4       0.25G      0.40G     0.60G    1.25G     yes
3B q4         1.50G      0.44G     0.60G    2.54G     yes
7B q4         3.50G      0.54G     0.60G    4.64G      no

At 4,096 tokens the 0.5B and 3B fixtures use about 1.25 and 2.54 GB respectively. The 7B fixture uses about 4.64 GB. Its ideally packed weights and assumed reserve already total 4.10 GB, so reducing only its context or KV head count cannot make it fit under these assumptions.

The figure uses the same memory calculation to show how much each component contributes and how far each candidate is from the device limit.

VisualizationOut [13]
CollapseExpand
Stacked memory bars for three candidates against a 4 GB limit line.
Calculated working memory for one 4,096-token session with ideally packed 4-bit weights, an fp16 cache, and a fixed reserve. The 7B configuration exceeds 4 GB.

Run the two candidates that fit on the same set of product questions. Keep retrieval results and the constrained decoder fixed as you measure supported answers, correct abstention, and valid field contents. Compare the 0.5B and 3B models on examples that need facts from more than one passage as well as single-passage lookup. The memory estimate leaves both in contention. Select the smaller candidate if it meets the defined quality and latency targets.

For the manual assistant, collect cases spanning one short source sentence, several nearby sentences, and passages from separate sections. Have a held-out set where a question cannot be answered from retrieval. Check whether the retrieved text contains the exact fact needed, whether the response obeys the schema, whether the answer is supported, and whether the escalation is correct. These checks distinguish a missing document from a retrieval ranking miss and from a generation error.

Define the behavior when the manual and the user's wording disagree. If a request asks how to use a feature unavailable in the current version, the expected response might identify the version mismatch and ask a follow-up. Record it as an example of correct refusal to provide instructions for that version even when the reply is fluent and schema-valid. Use the same behavior contract for both model candidates, and include that path when measuring end-to-end latency.

You can estimate one part of decode latency before measuring hardware. Suppose every generated token requires reading the stored weights and the whole KV cache once from memory, and the effective bandwidth is 200 GB/s. Divide the modeled traffic by that bandwidth. This scenario assumes one sequence at a 4,096-token cache, just like the memory check.

PythonIn [14]
CollapseExpand
def decode_ms_per_token(amounts, bandwidth_gbs=200):
    """Convert the modeled weights and cache traffic to milliseconds."""
    return 1000 * (amounts["weights"] + amounts["kv_cache"]) / bandwidth_gbs
ConsoleOut [15]
CollapseExpand
0.5B q4: 3.26 ms/token (306 tokens/s in the traffic model)
3B q4: 9.68 ms/token (103 tokens/s in the traffic model)
7B q4: 20.18 ms/token (50 tokens/s in the traffic model)

The calculation gives about 3.26, 9.68, and 20.18 ms per token for the three candidates. These rates describe that traffic assumption, with no allowance for compute, kernel launches, or retrieval. Cache reuse, batch execution, and the attention implementation can also change the traffic itself. Measure actual peak memory and both first-token and full-response latency on the intended device. Even at a measured 30 tokens/s, a 60-token decode takes two seconds before accounting for time spent retrieving passages and processing the prompt.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy