Course contentsShow
← AI Engineering
Lesson 654 of 1,88616. AI Agents - ImplementationFree lesson

Resource Limits and Timeouts

Implement CPU, memory, and execution time limits to prevent runaway tool executions from degrading system performance.

Resource Limits and Timeouts

What you'll learn: How to prevent agent tools from consuming excessive CPU, memory, or time and crashing your system.

Why Resource Limits Matter

When your AI agent executes tools—especially code execution or external API calls—you're handing control to processes that could misbehave. A tool might enter an infinite loop, allocate gigabytes of memory, or hang indefinitely waiting for a response. Without limits, a single runaway tool can freeze your entire agent or crash your server.

Think of resource limits like a safety harness: they let your agent explore and execute tools freely, but catch it before it falls off a cliff.

Core Components of Resource Control

CPU Limits: Cap the processor time a tool can consume (e.g., 50% of one core).

Memory Limits: Set maximum RAM allocation (e.g., 512MB per tool execution).

Execution Timeouts: Kill any tool that runs longer than a threshold (e.g., 30 seconds).

These constraints build on the sandboxing techniques you've already learned—Docker containers and isolated Python environments—but add quantitative guardrails.

Implementation Strategy

When invoking a tool:

  1. Set a timeout deadline before execution begins
  2. Configure container or process resource limits (CPU shares, memory cap)
  3. Monitor execution and forcibly terminate if limits are breached
  4. Return a clear error message to the agent explaining what happened

For Docker-based tools (which you've already explored), you can specify --memory="512m" and --cpus="0.5" flags. For Python code, libraries like resource module or process timeout wrappers enforce limits.

Graceful Degradation

When a tool hits a limit, don't just crash—return a structured error so the agent can reason about it and try a different approach or simpler tool.

Key Takeaway: Resource limits and timeouts are essential safety mechanisms that prevent individual tool executions from degrading or destroying your agent's performance, enabling reliable operation at scale.