Skip to main content
The ResourceLimits type is a TypedDict that configures resource constraints for Monty code execution. All limits are optional - omit a key to disable that limit.

Type Definition

Fields

int
Maximum number of heap allocations allowed. Execution will be terminated if this limit is exceeded.
float
Maximum execution time in seconds. Execution will be terminated if it exceeds this duration.
int
Maximum heap memory in bytes. Execution will be terminated if memory usage exceeds this limit.
int
Run garbage collection every N allocations. This helps control memory usage by periodically cleaning up unused objects.
int
Maximum function call stack depth. Default is 1000. This prevents infinite recursion from exhausting system resources.

Usage Examples

Basic Time Limit

Multiple Limits

Preventing Infinite Loops

Memory-Intensive Operations

Recursion Depth Control

Error Handling

When a resource limit is exceeded, a MontyRuntimeError is raised:

Best Practices

  1. Always set time limits when executing untrusted code to prevent infinite loops
  2. Use allocation limits to prevent memory exhaustion attacks
  3. Configure GC intervals for long-running or memory-intensive code
  4. Set recursion depth lower than default (1000) when running untrusted code
  5. Combine multiple limits for defense in depth
  6. Test limits with representative workloads to find appropriate values
  • Monty class - Main class that accepts ResourceLimits
  • Errors - Error types raised when limits are exceeded