// Blueprint 04 • Post-Training & Agentic FinOps

The Mathematics of QLoRA, DeepSpeed ZeRO-3 & Multi-Turn Agent Token Snowballing

By Anupam Das • Paired with the Fine-Tuning VRAM Calculator and Agent Cost Simulator

1. The 16-Byte Rule of Mixed-Precision Full Fine-Tuning

Engineers transitioning from inference to post-training often assume a 70B BF16 model (~140 GB inference weights) can be fine-tuned on two 80GB GPUs. Under standard mixed-precision training with AdamW, each trainable parameter requires 16 bytes of static state before counting activations:

For a 70.6B model, static training state alone totals 70.6 × 16 ≈ 1,130 GB. DeepSpeed ZeRO-3 (and PyTorch FSDP `FULL_SHARD`) partitions all three pools across N data-parallel GPUs, reducing per-GPU static memory to 16·P / N while gathering layer parameters on-the-fly over NVLink during forward and backward passes.

2. How QLoRA Compresses 70B SFT onto a Single 48GB GPU

QLoRA decouples frozen base representations from low-rank task updates:

Interactive Sizing: Model your exact dataset size, LoRA rank, and DeepSpeed cluster cost in the LLM Fine-Tuning VRAM & Training Cost Calculator →

3. Multi-Turn Agent Loops & Quadratic Token Snowballing

Stateless LLM APIs require an autonomous agent to re-transmit its accumulated message history on every tool-execution turn. Given static system + MCP schema tokens S and per-step growth Δ = T_obs + T_out across K steps, cumulative input tokens equal:

Cumulative_Input(K) = K * S + Δ * K * (K - 1) / 2

At K = 20 turns with S = 3,000 and Δ = 1,500, a single agent run bills 345,000 input tokens—nearly 10× the raw text generated during the session. Three architectural practices tame this curve:

  1. Strict Prefix Determinism: Keep system instructions and tool definitions byte-identical at index 0 (never inject dynamic `Date.now()` timestamps into the system prompt) so automatic prompt caching discounts 75%–90% of repeated turns.
  2. Sub-Agent Context Isolation: Delegate noisy file searches, log parsing, or web scraping to ephemeral sub-agents that return compact structured summaries rather than polluting the parent trajectory.
  3. Bounded Rolling Compaction: Summarize older tool outputs once history crosses 8–10 turns. Benchmark your exact savings in the Multi-Turn Agent Token Snowball Simulator →.

DeepGen Command Palette

Copied!