Smart Systems for Home

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

How to Choose a VM Size for Self-Hosted AI Agents

· 6 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

Choosing the right VM size for self-hosting AI agents is a balancing act between performance, cost, and reliability. Too small, and your agents will be slow or crash under load; too large, and you're paying for idle capacity. This guide walks you through the key factors—workload type, memory, CPU, GPU, and storage—and gives you a practical step-by-step method to pick a VM that fits your needs without overspending.

What You'll Need

  • A clear idea of what your AI agents do (e.g., text generation, image processing, data analysis, or simple automation).
  • An estimate of your expected concurrent users or tasks per minute.
  • Your cloud provider's VM pricing page (AWS, Azure, Google Cloud, or others) for reference.
  • Optional: a monitoring tool to track resource usage once deployed.

Step-by-Step

  1. Identify the primary workload type. AI agents fall into two broad categories: inference-heavy (running pre-trained models like LLMs or diffusion models) and logic-heavy (orchestrating workflows, calling APIs, processing data). Inference-heavy tasks need more CPU/GPU and memory; logic-heavy tasks are often more I/O and network bound.
  2. Estimate your concurrency. How many agents will run simultaneously? If you have 10 users each triggering an agent, and each agent takes 30 seconds of compute, you need enough resources to handle that burst. A good rule of thumb: plan for peak concurrency, not average.
  3. Determine memory (RAM) requirements. For LLM inference, a rough estimate is 1–2 GB of RAM per billion parameters (for quantized models). For example, a 7B parameter model might need 8–14 GB. For non-ML agents, 2–4 GB is often sufficient for basic automation, but add more if you're running multiple agents in containers.
  4. Choose CPU vs. GPU. If you're running large neural networks (e.g., LLMs with >1B parameters), a GPU is almost essential for reasonable latency. For smaller models or non-ML tasks, a modern CPU with 4–8 vCPUs is fine. If you're using cloud APIs (like OpenAI or Anthropic) but hosting your own orchestration logic, you don't need a GPU at all.
  5. Calculate storage needs. AI agents often need space for model weights, logs, and temporary data. Model weights can be several GB (e.g., 7B model is ~14 GB in fp16, ~4 GB in int4). Add 20–30% overhead for logs and intermediate files. Use SSD storage for faster model loading and I/O.
  6. Consider network bandwidth. If your agents fetch large datasets or stream responses, ensure your VM's network bandwidth is adequate. Most cloud providers offer 1–10 Gbps on standard instances; this is rarely a bottleneck unless you're moving terabytes.
  7. Pick a starting VM size. Based on the above, choose a baseline. For a simple agent (no ML), start with 2 vCPU / 4 GB RAM. For a small LLM (7B) with GPU, start with 4 vCPU / 16 GB RAM + a GPU with at least 8 GB VRAM (e.g., T4). For a large LLM (70B), you'll need multiple GPUs or a high-memory instance.
  8. Run a load test. Before committing, deploy your agent on the chosen VM and simulate realistic traffic using tools like Apache Bench, k6, or a simple script. Monitor CPU, memory, and latency. If CPU is at 90%+ or memory is maxed, scale up. If everything is under 50%, consider scaling down.
  9. Monitor and adjust. After going live, watch resource usage over a week. Cloud providers offer monitoring dashboards (e.g., CloudWatch, Azure Monitor). Adjust the VM size based on actual usage—scale up during peak hours, scale down during idle periods if using auto-scaling.

Common Mistakes

  • Overestimating GPU needs. Many agents don't need a GPU. If you're using external AI APIs, a CPU-only instance is cheaper and often faster for orchestration tasks.
  • Ignoring memory bandwidth. For CPU-based inference, memory bandwidth (GB/s) matters more than core count. A VM with many vCPUs but low memory bandwidth will be slow for large models.
  • Forgetting about cold starts. If your agent loads a model on startup, a VM with slower storage will have longer cold start times. Use SSDs and consider keeping the VM warm.
  • Under-provisioning for bursts. Agents often have unpredictable spikes. A small VM might handle average load but crash during a spike. Always plan for at least 2x your expected peak.
  • Not considering region latency. If your users are in Europe, a VM in the US adds latency. Choose a region close to your user base to reduce response times.

Tips

  • Start small, scale up. It's easier to upgrade a VM than to downgrade (which often requires reconfiguration). Begin with a modest size and increase as needed.
  • Use auto-scaling if possible. Many cloud providers offer auto-scaling groups. This lets you handle spikes without paying for idle capacity.
  • Leverage spot/preemptible instances for non-critical workloads. If your agents can tolerate interruptions, spot instances can save 60–90% on compute costs.
  • Consider containerization. Running agents in Docker or Kubernetes makes it easier to move between VM sizes and cloud providers.
  • Quantize models to reduce memory. Using int4 or int8 quantization can cut memory usage by 4x, allowing a smaller VM.

Troubleshooting

  • Agent is slow or times out. Check CPU and memory usage. If memory is at 100%, increase RAM. If CPU is pegged, consider more vCPUs or a GPU. Also check if the model is being loaded from disk on every request—cache it in memory.
  • Out-of-memory (OOM) errors. This means your VM doesn't have enough RAM. Reduce model size (quantize), increase swap (not recommended for production), or move to a larger instance.
  • GPU memory exhausted. Reduce batch size, use gradient checkpointing, or choose a GPU with more VRAM. If using a library like Hugging Face, set low_cpu_mem_usage=True.
  • High latency even with low CPU. This could be a network issue or a bottleneck in your code (e.g., synchronous calls). Profile your agent to find the slow part. If it's network, consider a VM with higher bandwidth or moving to a region closer to your API endpoints.
  • Costs are higher than expected. Review your usage—you might be paying for idle time. Use auto-scaling to shut down when not in use, or switch to a smaller instance for development.

Frequently Asked Questions

Do I need a GPU for self-hosting AI agents?

Only if you're running large models locally. If you use cloud AI APIs, a CPU-only VM is sufficient and more cost-effective.

How much RAM do I need for a 7B parameter LLM?

With 4-bit quantization, you can run a 7B model in about 4–6 GB of RAM. Without quantization, you'd need 14+ GB.

What's the difference between vCPUs and physical cores?

A vCPU is a virtual core; on most clouds, 1 vCPU equals 1 hyper-thread of a physical core. For AI workloads, physical cores matter more than hyper-threads, so check the instance's specs.

Can I change VM size later without reconfiguring everything?

Yes, most cloud providers allow resizing, but you'll need to stop the VM, change the size, and restart. Your data persists, but the IP may change unless you have a static IP.

Is it cheaper to use spot instances for AI agents?

Spot instances are much cheaper, but they can be terminated at any time. They're suitable for batch processing or non-critical agents, not for real-time services.

How do I monitor VM resource usage?

Use your cloud provider's built-in monitoring (e.g., AWS CloudWatch, Azure Monitor) or install tools like Prometheus and Grafana for more detailed metrics.