# How to Build an LLM Agent That Can Write and Run Code

A guide to creating an AI agent capable of writing and executing code using OpenAI Agents SDK and Docker.

- **Source:** [Towards Data Science](https://towardsdatascience.com)
- **Published:** 2026-07-22
- **Topic:** [Data & AI](https://brifing.si/c/data-ai)
- **Original article:** <https://towardsdatascience.com/build-an-llm-agent-that-can-write-and-run-code/>
- **On Brifing:** https://brifing.si/s/06e14104-8e02-417f-afbe-0c446c59cceb

## The Power of Code-Executing Agents

AI agents that can write and execute code are revolutionizing fields from data analysis to automation. With this capability, agents can inspect files, write data processing logic, and generate reports. OpenAI Agents SDK now makes it possible to develop such agents.

## Core Components

A code-executing agent has three core components: the model, the workspace, and the execution environment. The model is powered by an LLM, the workspace manages input and output files, and the execution environment safely runs the generated code.

## The Role of the Model

The LLM acts as the brain of the agent, inspecting available files, deciding on the type of analysis needed, and writing the necessary code. It can also read results and iterate to improve the process.

## Workspace

The workspace is where users can upload data files (CSV, PDF, etc.) and the agent can work on them to produce outputs. This space simplifies the management of inputs and outputs.

## Execution Environment

The LLM agent only writes code; it doesn't execute it. A secure execution environment, often Docker, is required to run the generated code safely.

## Control Mechanisms

Safety controls are crucial for the agent's operation. At the model level, system instructions are defined, while the workspace level determines file usage, and the execution environment manages dependencies.

## OpenAI Agents SDK

The OpenAI Agents SDK offers code execution capabilities through SandboxAgent. This agent configures the workspace using a Manifest to specify which files to use and integrates seamlessly with Docker.

## Docker Integration

Docker provides an ideal environment for safely executing code. With OpenAI Agents SDK, you can create a custom workspace using Docker and pre-install necessary dependencies.

## Case Study: CSV Analysis

As a case study, an agent can analyze a CSV file to detect anomalies in energy consumption. The agent inspects the data and generates reports and charts.

## Conclusion and Recommendations

LLM agents capable of executing code hold great potential in data analysis and automation. Tools like OpenAI Agents SDK and Docker offer effective methods to develop such agents and optimize workflows.

## Sources covering this story

- https://towardsdatascience.com/build-an-llm-agent-that-can-write-and-run-code/ — 

---

_Summary written by Brifing from the original reporting. Copyright in the original article belongs to its publisher; read the full piece at the link above._
