Skip to main content
This example demonstrates building an AI agent that can write Python code and execute it to solve problems.

Features

  • Code execution in a sandboxed environment
  • Iterative problem-solving
  • Error handling and retry logic
This example executes arbitrary code. In production, use a proper sandbox environment like Docker containers or restricted execution environments.

Complete Code

How It Works

  1. Problem Statement - User describes what they want to compute
  2. Code Generation - AI writes Python code to solve the problem
  3. Execution - Code runs in a sandboxed environment
  4. Result - AI explains the output

Sample Output

Security Considerations

Never run arbitrary code in production without proper sandboxing!

Safer Alternatives

Run code in isolated Docker containers with resource limits.
Use the RestrictedPython library for safer execution.
Send code to a sandboxed execution service.

Restricted Builtins

The example limits available functions:

Customization Ideas

Add tools for reading/writing files in a sandboxed directory.
Allow importing specific safe packages like math or json.
Create a REPL-style interface for iterative coding.