Course contentsShow
← AI Engineering
Lesson 1501 of 1,88636. Security in AI SystemsFree lesson

Resource Limits and DoS Prevention

Setting CPU, memory, and execution time limits to prevent resource exhaustion from malicious or buggy code.

Resource Limits and DoS Prevention

What You'll Learn

You'll learn how to set CPU, memory, and execution time limits to prevent malicious or buggy AI-generated code from exhausting system resources and causing denial-of-service conditions.

Why Resource Limits Matter

When your AI system generates and executes code in a sandbox (as you learned in the previous lessons), you've isolated where the code runs—but you haven't yet constrained how much it can consume. A malicious user could prompt your system to generate infinite loops, memory-hogging algorithms, or CPU-intensive operations that monopolize resources and block legitimate requests.

Think of it like a restaurant with private dining rooms (sandboxes): you've given each customer their own space, but without time limits or capacity controls, one party could camp out all night, preventing others from using the room.

Core Resource Limits

CPU Time: Set maximum execution duration (e.g., 5 seconds). Kills processes that run too long, preventing infinite loops.

Memory: Cap RAM usage (e.g., 512MB). Prevents memory exhaustion attacks where code allocates gigabytes until the system crashes.

Process Count: Limit spawned subprocesses. Stops fork bombs that create thousands of processes to overwhelm the system.

Disk I/O: Restrict file operations and storage. Prevents filling disk space or excessive read/write operations.

Implementation Approach

Most sandboxing tools you've already learned (Docker, serverless functions, process isolation) support resource limits through configuration. You specify maximums before execution begins, and the runtime enforces them automatically, terminating violating processes.

This is your last line of defense: even if input validation and code analysis miss malicious patterns, resource limits ensure the damage stays contained and your system remains available for other users.

Key Takeaway: Resource limits—CPU time, memory caps, and execution constraints—are essential safeguards that prevent individual requests from exhausting system resources and causing denial-of-service, complementing sandbox isolation with quantitative controls.