HyperAIHyperAI

Command Palette

Search for a command to run...

6 hours ago
OpenAI
Agent
LLM

Build a Code-Executing LLM Agent Using OpenAI Agents SDK

Developing Large Language Model agents capable of writing and executing code significantly expands their utility, enabling complex tasks such as data inspection, processing logic generation, and artifact creation. A guide demonstrates this pattern using the OpenAI Agents SDK and Docker, establishing a reusable framework for agentic applications. A code-executing agent relies on three core components: the model, the workspace, and the execution environment. The model powers reasoning and code generation, while the workspace manages input and output files. The execution environment runs the generated code in isolation. The OpenAI Agents SDK implements this via the SandboxAgent class, which orchestrates these elements. Developers define a Manifest to stage local files into the sandbox, configure the agent with specific instructions and models, and link the agent to a Docker-based execution session. Implementation begins by preparing a Docker container as the runtime. This container includes necessary dependencies, such as Python and data science libraries, pre-installed to ensure efficiency. The workspace is configured via a Manifest, mapping local files to sandbox paths. Input data files are staged as resources. The agent is then instantiated with clear instructions regarding its role, available tools, and expected outputs. Execution occurs through a sandbox session created via the Docker client, where the agent iteratively writes, runs, and refines code. Upon completion, the workspace can be persisted and extracted to retrieve artifacts like reports, charts, or processed data files back to the host machine. To validate the pattern, a case study involved a time series anomaly detection task using a synthetic building energy dataset. The agent was tasked with inspecting a CSV file containing hourly energy consumption and temperature data, identifying abnormal usage events, and generating output files including an anomaly report, a list of detected events, and a visualization chart. The agent successfully analyzed the data, computed robust z-scores to flag outliers, and produced the requested artifacts, demonstrating the ability to handle multi-step analysis without direct human intervention. Successful deployment of code-executing agents requires attention to control and configuration. Developers should start with explicit agent instructions defining the role, task, and outcome. File staging must be precisely defined to ensure the agent accesses required data. Runtime dependencies should be curated within the Docker image, and output file names and locations should be explicitly stated to facilitate result extraction. This pattern supports a wide range of workflows, from data analysis to automated coding tasks, providing a robust foundation for advanced agentic systems.

Related Links