Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Estimate GPU Capacity for Concurrent AI Agent Sessions

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Estimating GPU capacity for concurrent AI agent sessions means figuring out how many agents your GPU can run at once without running out of memory or slowing to a crawl. This guide gives you a practical, step-by-step method to calculate that number based on your model size, context length, and GPU memory. You'll need your GPU's total VRAM, the model you plan to run, and a rough idea of how many tokens each agent session will use.

What You'll Need

  • GPU model and its total VRAM (e.g., 24 GB for an RTX 3090)
  • The AI model you intend to run (e.g., Llama 3 8B, Mistral 7B)
  • Estimated average context length per agent session (in tokens)
  • Expected number of concurrent agent sessions (your target)
  • A basic calculator or spreadsheet

Step-by-Step

  1. Find your GPU's total VRAM. Check your GPU specs (e.g., 24 GB for RTX 3090, 16 GB for RTX 4080). This is your hard ceiling.
  2. Determine the model's memory footprint for weights. A model with N billion parameters uses roughly 2 bytes per parameter in FP16, so an 8B model needs about 16 GB just for weights. If you use 4-bit quantization, it's about 0.5 bytes per parameter, so 8B ≈ 4 GB.
  3. Calculate per-session KV cache memory. The KV cache stores attention keys/values. For a transformer with L layers, H hidden size, and N heads, the KV cache per token is roughly 2 (key and value) × L × H × 2 bytes (FP16). For Llama 3 8B (L=32, H=4096), that's 2 × 32 × 4096 × 2 = 524,288 bytes ≈ 0.5 MB per token. Multiply by your average context length (e.g., 4,000 tokens) to get ~2 GB per session.
  4. Add overhead for activations and CUDA context. Reserve about 1–2 GB for the CUDA context and activation memory. This is a fixed cost per process, not per session.
  5. Compute total memory per session. Add the per-session KV cache to the model weights divided by the number of concurrent sessions you want. For example, with a 16 GB weights model (FP16) and 3 concurrent sessions, each session's share of weights is 16/3 ≈ 5.3 GB, plus 2 GB KV cache = 7.3 GB per session.
  6. Apply a safety margin. Subtract 10–15% from your total VRAM to avoid OOM (out-of-memory) errors from fragmentation and driver overhead. For a 24 GB card, that leaves ~20.4 GB usable.
  7. Divide usable VRAM by per-session memory. Using the example above: 20.4 GB / 7.3 GB ≈ 2.8, so you can safely run 2 concurrent sessions, maybe 3 if you're careful.
  8. Test empirically. Run one session, monitor VRAM usage with nvidia-smi, then incrementally add sessions until you hit the limit. This validates your estimate.

Common Mistakes

  • Ignoring KV cache growth: Many people only count weights. For long contexts, KV cache can dominate memory.
  • Using FP32 instead of FP16: Some frameworks default to FP32, doubling weight memory. Always check your precision.
  • Forgetting CUDA context: Each process (or each session if run as separate processes) consumes 500 MB to 1 GB just for CUDA context.
  • Assuming linear scaling: GPU memory doesn't scale perfectly with sessions due to fragmentation and shared overhead.

Tips

  • Use quantization: 4-bit quantization (e.g., GPTQ, AWQ) can cut weight memory by 4x, allowing more sessions.
  • Limit context length: If your agents don't need long conversations, cap the context to reduce KV cache.
  • Batch sessions into one process: Running multiple agents in a single process (e.g., with a batching inference server) shares CUDA context and reduces overhead.
  • Monitor with nvidia-smi: Use nvidia-smi -l 1 to watch real-time memory usage.

Troubleshooting

  • Out-of-memory errors: Reduce the number of sessions, lower context length, or switch to a smaller quantized model.
  • Slow inference: If memory is fine but latency is high, your GPU is compute-bound. Reduce sessions or use a faster model.
  • Memory usage higher than expected: Check if your framework is using FP32 or if there are multiple copies of the model. Use torch.cuda.memory_summary() in PyTorch to see breakdown.
  • Inconsistent results: Some sessions may fail randomly. This could be due to memory fragmentation—try restarting the process or using a smaller batch of sessions.

Frequently Asked Questions

How much VRAM do I need for 10 concurrent agents?

It depends on model size and context. For a 7B model in 4-bit (4 GB weights) with 2 GB KV cache per session, 10 sessions need ~24 GB for weights (shared) plus 20 GB KV cache, so at least 44 GB. You'd need a multi-GPU setup or a larger card like an A100 80GB.

Can I run multiple agents on a single GPU with a consumer card?

Yes, if you use a quantized model and keep context short. A 24 GB card can run 3–4 sessions of a 7B model with 2K context, but not many more.

Does batching help with concurrent agents?

Yes, batching shares weight memory and CUDA context, so you can run more sessions per GB. However, each session still needs its own KV cache.

What's the difference between concurrent and sequential agents?

Concurrent agents run simultaneously, requiring memory for all at once. Sequential agents run one after another, so you only need memory for one at a time.

How do I estimate KV cache size for my specific model?

Use the formula: 2 × layers × hidden_size × 2 bytes per token. For a model with 32 layers and 4096 hidden size, that's ~0.5 MB per token. Multiply by your average tokens per session.

Is it better to use a larger GPU or multiple smaller ones?

For concurrent sessions, a single large GPU is simpler because you avoid inter-GPU communication. Multiple GPUs can help if you need more total memory, but you'll need to split the model or use tensor parallelism, which adds complexity.