Build an LLM Agent with Code Execution Capabilities using OpenAI SDK and Docker
- Authors

- Name
- Nino
- Occupation
- Senior Tech Editor
The evolution of Large Language Models (LLMs) has moved rapidly from simple chat interfaces to sophisticated agents capable of interacting with the physical and digital world. One of the most powerful capabilities an agent can possess is the ability to write and execute code. This transforms an LLM from a text generator into a problem-solving engine capable of data analysis, mathematical computation, and automated system management. In this guide, we will explore how to build a robust code-executing agent using the OpenAI Agents SDK and Docker for secure sandboxing.
To build a reliable agent, you need a high-performance backend. Services like n1n.ai provide the low-latency, high-reliability API access required to ensure your agent's 'thought process' isn't interrupted by provider downtime or rate limits.
The Architecture of a Code-Executing Agent
A code-executing agent operates on a feedback loop often referred to as the 'Plan-Act-Observe' cycle. Unlike standard RAG (Retrieval-Augmented Generation) systems that merely fetch text, a code agent follows these steps:
- Reasoning: The LLM analyzes the user prompt and determines that code is required to solve the problem (e.g., 'Calculate the compound interest for $10,000 over 5 years').
- Generation: The LLM writes a Python script to perform the calculation.
- Execution: The system sends this script to a secure environment (Docker).
- Observation: The output of the script (or the error message) is sent back to the LLM.
- Refinement: If the code failed, the LLM uses the error log to fix the script and try again.
Why Docker is Mandatory for Code Execution
Allowing an LLM to run arbitrary code on your host machine is a massive security risk. An agent could accidentally (or maliciously) delete files, access environment variables, or launch network attacks. Docker provides a lightweight, isolated container that acts as a sandbox. If the agent runs rm -rf /, it only destroys the temporary container, leaving your host system untouched.
Step 1: Setting Up the Environment
First, ensure you have Docker installed and the OpenAI Agents SDK. You will also need an API key from a stable provider. We recommend using n1n.ai to access the latest GPT-4o or o1 models, which are particularly adept at code generation.
# Install the necessary libraries
# pip install openai docker
import docker
import openai
import os
# Configure your client via n1n.ai
client = openai.OpenAI(
api_key="YOUR_N1N_API_KEY",
base_url="https://api.n1n.ai/v1"
)
Step 2: Implementing the Docker Sandbox
We need a function that takes a string of Python code, runs it inside a Docker container, and returns the result. Using the python:3.9-slim image ensures a small footprint and fast startup times.
def execute_python_code(code_string):
docker_client = docker.from_env()
container = docker_client.containers.run(
image="python:3.9-slim",
command=f"python3 -c \"{code_string}\"",
detach=True,
network_disabled=True, # Security: No internet for the sandbox
mem_limit="128m" # Resource limit
)
# Wait for completion and get logs
result = container.wait()
logs = container.logs().decode("utf-8")
container.remove()
return logs if result["StatusCode"] == 0 else f"Error: {logs}"
Step 3: Defining the Agent Tools
Using the OpenAI Agents SDK, we define the execute_python_code function as a tool that the model can call. This is the 'bridge' between the LLM's reasoning and the Docker execution.
tools = [
{
"type": "function",
"function": {
"name": "execute_python_code",
"description": "Executes Python code in a sandbox and returns the output.",
"parameters": {
"type": "object",
"properties": {
"code_string": {
"type": "string",
"description": "The Python code to execute."
}
},
"required": ["code_string"]
}
}
}
]
Step 4: The Agentic Loop
Now we create the loop that allows the agent to think and act. When the LLM decides to use a tool, we intercept that call, run it in Docker, and feed the result back into the conversation history.
def run_agent(prompt):
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto"
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
if tool_calls:
messages.append(response_message)
for tool_call in tool_calls:
function_args = json.loads(tool_call.function.arguments)
print(f"Executing Code: {function_args['code_string']}")
execution_result = execute_python_code(function_args['code_string'])
messages.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": "execute_python_code",
"content": execution_result,
})
# Get the final response from the LLM after seeing the code output
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
return final_response.choices[0].message.content
return response_message.content
Pro Tip: Handling Complex Dependencies
If your agent needs specific libraries like pandas or numpy, you should create a custom Dockerfile. This prevents the agent from having to run pip install every time, which is slow and requires internet access (a security risk). Pre-build an image with all necessary data science tools and use that image in your execute_python_code function.
Scaling and Latency
When deploying these agents in production, latency becomes the primary bottleneck. Every 'turn' in the agent loop adds seconds to the user experience. This is why selecting a provider like n1n.ai is critical. By offering aggregated access to the fastest global models with optimized routing, n1n.ai minimizes the overhead of the API call, allowing the agent to 'think' and 'act' in near real-time.
Conclusion
Building a code-executing agent is a significant step toward true AI autonomy. By combining the reasoning power of models like GPT-4o with the safety of Docker containers, you can create tools that don't just talk about data, but actually process it. Whether you are building an automated DevOps assistant or a sophisticated financial analyst, the combination of a secure sandbox and a high-speed API provider is the foundation of success.
Get a free API key at n1n.ai