AI-powered code generation, execution, review, and refactoring
Generate, run, review, and fix code with the CodeAgent — a specialised agent with sandboxed execution built in.
from praisonaiagents import CodeAgentagent = CodeAgent(name="Coder")code = agent.generate("Write a function to calculate fibonacci numbers")result = agent.execute("print('Hello, World!')")print(result["stdout"])
The CodeAgent provides comprehensive code assistance including generation, execution in sandboxed environments, code review, explanation, refactoring, and bug fixing.
from praisonaiagents import CodeAgentagent = CodeAgent(name="Coder")code = agent.generate("Write a function to calculate fibonacci numbers")result = agent.execute("print('Hello, World!')")print(result["stdout"])
2
With Configuration
Lock down the sandbox for production use.
from praisonaiagents import CodeAgent, CodeConfigconfig = CodeConfig( sandbox=True, timeout=30, allowed_languages=["python"],)agent = CodeAgent( name="SafeCoder", llm="gpt-4o-mini", code=config, instructions="Write clean, well-documented Python code",)code = agent.generate("Create a class for managing a todo list")print(agent.review(code))
from praisonaiagents import CodeAgentagent = CodeAgent(name="Generator")# Generate Python codecode = agent.generate( "Write a function that sorts a list using quicksort", language="python")print(code)
result = agent.execute(code, language="python")# Result structure{ "stdout": "...", # Standard output "stderr": "...", # Standard error "return_code": 0, # Exit code "execution_time": 0.5 # Time in seconds}
Leave sandbox=True for any untrusted or model-generated code. Disabling isolation in production lets arbitrary code touch your host — a serious security risk.
Set a timeout that fits the task
A short timeout stops runaway loops from hanging your process. Tune it to the longest legitimate run your code needs, not longer.
Review before you execute
Call review() on generated code before execute(). Catching an obvious bug in review is cheaper than debugging a failed run.
Always check return codes and stderr
execute() returns return_code and stderr. Branch on them — a non-zero code means the run failed, and fix() can repair it using the error text.