Build an LLM Agent That Can Write and Run Code
Summary
This article details building a code-executing LLM agent using the OpenAI Agents SDK and Docker, enabling capabilities like file inspection, data processing, and artifact generation. It outlines three core components: the LLM model (e.g., "gpt-5.4"), a workspace for input/output files, and an isolated Docker execution environment. A practical case study demonstrates an agent analyzing an hourly building energy CSV dataset ("building_energy.csv") to detect anomalies. The agent, configured with a Docker image containing "numpy", "scipy", "pandas", and "matplotlib", successfully identifies an abnormal energy event and produces "anomalies.csv", "energy_anomalies.png", and "anomaly_report.md" by writing and executing Python code within its sandbox. The process involves defining a "Manifest" for file staging and configuring "DockerSandboxClientOptions" with a custom image.
Key takeaway
For AI Engineers building autonomous LLM applications, integrating code execution capabilities is crucial for expanding agent utility beyond text generation. You should utilize the OpenAI Agents SDK with Docker to create isolated, reproducible environments for tasks like data analysis or file manipulation. Pre-installing necessary libraries in your Docker image and explicitly defining expected outputs will streamline development and ensure reliable artifact generation from your agents.
Key insights
LLM agents achieve powerful code execution by integrating with sandboxed environments like Docker via SDKs.
Principles
- Code execution significantly expands agent capabilities.
- Isolate agent code execution in a sandbox.
- Explicitly define agent's role, task, and expected outputs.
Method
Configure a "SandboxAgent" with a "Manifest" for file staging and a Docker sandbox. Run the agent, allowing it to execute code within the isolated environment, then persist the workspace to retrieve generated artifacts.
In practice
- Use "openai-agents[docker]" for local sandbox.
- Pre-install dependencies in Docker image.
- Specify output file names and locations.
Topics
- LLM Agents
- Code Execution
- OpenAI Agents SDK
- Docker Sandbox
- Data Analysis
- Anomaly Detection
- Python Runtime
Best for: AI Engineer, Machine Learning Engineer, MLOps Engineer
Related on AIssential
See Counsel's argued verdicts on the open AI decisions leaders are weighing →
Editorial summary, takeaway, and curation by AIssential. Original article published by Towards Data Science.