Build an LLM Agent That Can Write and Run Code

· Source: Towards Data Science · Field: Technology & Digital — Artificial Intelligence & Machine Learning, Software Development & Engineering, Data Science & Analytics · Depth: Intermediate, medium

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

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

Topics

Best for: AI Engineer, Machine Learning Engineer, MLOps Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Towards Data Science.