Skip to main content
Connect any LLM to Hopx Sandboxes for safe code execution. Build ChatGPT-style code interpreters, AI coding assistants, and autonomous agents.
Hopx works with any LLM - use tool calling (function calling) or prompt the model to output code that you execute safely.

Quick Start

The easiest way is to use the LLM’s tool calling capabilities (also called function calling):
  1. Define a tool/function for execute_code
  2. Let the LLM call it when needed
  3. Execute the code in a Hopx Sandbox
  4. Return results to the LLM

OpenAI

Install

pip install hopx-ai
Plus OpenAI SDK:
pip install openai

Simple Approach

from openai import OpenAI
from hopx_ai import Sandbox

# Create OpenAI client
client = OpenAI()

system = """You are a helpful assistant that can execute Python code.
Only respond with the code to be executed. No explanations, just code."""

prompt = "Calculate how many r's are in the word 'strawberry'"

# Get code from GPT-4
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": system},
        {"role": "user", "content": prompt}
    ]
)

# Extract code
code = response.choices[0].message.content

# Execute in Hopx Sandbox
with Sandbox.create(template="code-interpreter") as sandbox:
    execution = sandbox.run_code(code)
    result = execution.stdout
    
print(result)
# Output: There are 3 r's in 'strawberry'
import json
from openai import OpenAI
from hopx_ai import Sandbox

client = OpenAI()
model = "gpt-4o"

# Define messages
messages = [{
    "role": "user",
    "content": "Analyze this sales data and create a chart: [100, 150, 120, 180, 200]"
}]

# Define the code execution tool
tools = [{
    "type": "function",
    "function": {
        "name": "execute_python",
        "description": "Execute Python code in a secure sandbox and return results",
        "parameters": {
            "type": "object",
            "properties": {
                "code": {
                    "type": "string",
                    "description": "The Python code to execute"
                }
            },
            "required": ["code"]
        }
    }
}]

# Get initial response
response = client.chat.completions.create(
    model=model,
    messages=messages,
    tools=tools
)

response_message = response.choices[0].message
messages.append(response_message)

# Execute tool if called
if response_message.tool_calls:
    for tool_call in response_message.tool_calls:
        if tool_call.function.name == "execute_python":
            # Execute code in Hopx
            with Sandbox.create(template="code-interpreter") as sandbox:
                code = json.loads(tool_call.function.arguments)['code']
                execution = sandbox.run_code(code)
                result = execution.stdout
                
                # Download any generated images
                if hasattr(execution, 'rich_outputs') and execution.rich_outputs:
                    for output in execution.rich_outputs:
                        if output.type == "image":
                            img_data = sandbox.files.read(output.path)
                            with open(f"chart_{output.filename}", "wb") as f:
                                f.write(img_data)
                            result += f"\n✅ Chart saved: chart_{output.filename}"
            
            # Send result back to GPT
            messages.append({
                "role": "tool",
                "name": "execute_python",
                "content": result,
                "tool_call_id": tool_call.id
            })

# Get final response
final_response = client.chat.completions.create(
    model=model,
    messages=messages
)

print(final_response.choices[0].message.content)

Anthropic Claude

Simple Approach

from anthropic import Anthropic
from hopx_ai import Sandbox

client = Anthropic()

system = """You are a helpful assistant that can execute Python code.
Only respond with code. No explanations, just code."""

prompt = "Create a scatter plot of random data with matplotlib"

# Get code from Claude
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "assistant", "content": system},
        {"role": "user", "content": prompt}
    ]
)

code = response.content[0].text

# Execute in Hopx
with Sandbox.create(template="code-interpreter") as sandbox:
    execution = sandbox.run_code(code)
    print(execution.stdout)
from anthropic import Anthropic
from hopx_ai import Sandbox

client = Anthropic()
model = "claude-3-5-sonnet-20241022"

messages = [{
    "role": "user",
    "content": "Analyze sales data: [45000, 52000, 48000, 61000, 58000]. Create a visualization."
}]

tools = [{
    "name": "execute_python",
    "description": "Execute Python code in a secure sandbox with NumPy, Pandas, Matplotlib",
    "input_schema": {
        "type": "object",
        "properties": {
            "code": {
                "type": "string",
                "description": "Python code to execute"
            }
        },
        "required": ["code"]
    }
}]

# Get Claude's response
message = client.messages.create(
    model=model,
    max_tokens=1024,
    messages=messages,
    tools=tools
)

messages.append({
    "role": "assistant",
    "content": message.content
})

# Execute tool if called
if message.stop_reason == "tool_use":
    tool_use = next(block for block in message.content if block.type == "tool_use")
    
    if tool_use.name == "execute_python":
        with Sandbox.create(template="code-interpreter") as sandbox:
            code = tool_use.input['code']
            execution = sandbox.run_code(code)
            result = execution.stdout
        
        messages.append({
            "role": "user",
            "content": [{
                "type": "tool_result",
                "tool_use_id": tool_use.id,
                "content": result
            }]
        })

# Get final response
final = client.messages.create(
    model=model,
    max_tokens=1024,
    messages=messages,
    tools=tools
)

print(final.content[0].text)

Google Gemini

import google.generativeai as genai
from hopx_ai import Sandbox

genai.configure(api_key="YOUR_API_KEY")

# Define the code execution function
def execute_python(code: str) -> str:
    """Execute Python code in a sandbox"""
    with Sandbox.create(template="code-interpreter") as sandbox:
        execution = sandbox.run_code(code)
        return execution.stdout

# Create model with function calling
model = genai.GenerativeModel(
    'gemini-1.5-pro',
    tools=[execute_python]
)

# Start chat
chat = model.start_chat()

response = chat.send_message(
    "Calculate the first 10 Fibonacci numbers and plot them"
)

print(response.text)

Vercel AI SDK

Build AI applications with Vercel’s AI SDK:
import { openai } from '@ai-sdk/openai';
import { generateText, tool } from 'ai';
import { Sandbox } from '@hopx-ai/sdk';
import { z } from 'zod';

// Define code execution tool
const executePythonTool = tool({
  description: 'Execute Python code in a secure sandbox',
  parameters: z.object({
    code: z.string().describe('Python code to execute')
  }),
  execute: async ({ code }) => {
    const sandbox = await Sandbox.create({ template: 'code-interpreter' });
    
    try {
      const execution = await sandbox.runCode(code);
      return {
        success: execution.success,
        stdout: execution.stdout,
        stderr: execution.stderr
      };
    } finally {
      await sandbox.kill();
    }
  }
});

// Use with Vercel AI SDK
const { text } = await generateText({
  model: openai('gpt-4o'),
  tools: {
    execute_python: executePythonTool
  },
  prompt: 'Create a bar chart showing monthly sales: [45k, 52k, 48k, 61k, 58k, 67k]'
});

console.log(text);

LangChain

Simple Chain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from hopx_ai import Sandbox

system = "You are a Python code generator. Only output code, no explanations."

# Create chain
llm = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_messages([
    ("system", system),
    ("human", "{input}")
])
output_parser = StrOutputParser()
chain = prompt | llm | output_parser

# Generate code
code = chain.invoke({
    "input": "Calculate mean, median, std of [12, 45, 23, 67, 34, 89, 56]"
})

# Execute in Hopx
with Sandbox.create(template="code-interpreter") as sandbox:
    result = sandbox.run_code(code)
    print(result.stdout)

Agent with Tools

from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from hopx_ai import Sandbox

# Define tool
@tool
def execute_python(code: str):
    """Execute Python code in a secure sandbox"""
    with Sandbox.create(template="code-interpreter") as sandbox:
        execution = sandbox.run_code(code)
        return execution.stdout

# Create agent
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a data analysis assistant with Python execution capabilities."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}")
])

llm = ChatOpenAI(model="gpt-4o", temperature=0)
agent = create_tool_calling_agent(llm, [execute_python], prompt)
agent_executor = AgentExecutor(agent=agent, tools=[execute_python], verbose=True)

# Run agent
result = agent_executor.invoke({
    "input": "Analyze these sales figures and find the trend: [100, 120, 115, 140, 135, 160]"
})

print(result['output'])

LlamaIndex

from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
from llama_index.core.agent import ReActAgent
from hopx_ai import Sandbox

# Define tool
def execute_python(code: str) -> str:
    """Execute Python code in a secure sandbox"""
    with Sandbox.create(template="code-interpreter") as sandbox:
        execution = sandbox.run_code(code)
        return execution.stdout

tool = FunctionTool.from_defaults(
    name="execute_python",
    description="Execute Python code and return results",
    fn=execute_python
)

# Create agent
llm = OpenAI(model="gpt-4o")
agent = ReActAgent.from_tools([tool], llm=llm, verbose=True)

# Run agent
response = agent.chat(
    "Calculate the correlation between these two datasets: X=[1,2,3,4,5], Y=[2,4,5,4,5]"
)

print(response)

CrewAI

Build multi-agent systems:
from crewai import Agent, Task, Crew
from hopx_ai import Sandbox

def execute_code(code):
    """Execute Python code in Hopx"""
    with Sandbox.create(template="code-interpreter") as sandbox:
        execution = sandbox.run_code(code)
        return execution.stdout

# Create data analyst agent
data_analyst = Agent(
    role='Data Analyst',
    goal='Analyze data and create visualizations',
    backstory='Expert in Python, Pandas, and data visualization',
    tools=[execute_code],
    verbose=True
)

# Create task
analysis_task = Task(
    description="""Analyze this sales data and create a comprehensive report:
    Q1: $45,000, Q2: $52,000, Q3: $48,000, Q4: $61,000
    Include growth rate, trends, and a visualization.""",
    agent=data_analyst
)

# Create crew
crew = Crew(
    agents=[data_analyst],
    tasks=[analysis_task],
    verbose=True
)

# Run
result = crew.kickoff()

print(result)

Ollama (Local Models)

Run local LLMs with Ollama:
import ollama
from hopx_ai import Sandbox

# Send prompt to local model
response = ollama.chat(
    model="llama3.2",
    messages=[{
        "role": "system",
        "content": "You are a Python code generator. Only output code."
    }, {
        "role": "user",
        "content": "Create a function to calculate factorial of a number"
    }]
)

code = response['message']['content']

# Execute in Hopx
with Sandbox.create(template="code-interpreter") as sandbox:
    execution = sandbox.run_code(code)
    print(execution.stdout)

Best Practices

Never execute code blindly. Validate it first:
def is_safe_code(code: str) -> bool:
    """Basic safety checks"""
    dangerous = ['os.system', 'subprocess', '__import__', 'eval(']
    return not any(d in code for d in dangerous)

if is_safe_code(code):
    result = sandbox.run_code(code)
Always check execution success:
execution = sandbox.run_code(code)
if execution.success:
    return execution.stdout
else:
    return f"Error: {execution.stderr}"
Prevent infinite loops:
result = sandbox.run_code(code, timeout=30)
For multiple executions in a session:
with Sandbox.create(template="code-interpreter") as sandbox:
    result1 = sandbox.run_code(code1)
    result2 = sandbox.run_code(code2)  # Reuse same sandbox

Production Example

Complete ChatGPT-style code interpreter:
from openai import OpenAI
from hopx_ai import Sandbox
import json

class CodeInterpreter:
    def __init__(self, api_key: str):
        self.client = OpenAI(api_key=api_key)
        self.sandbox = None
        self.conversation = []
        
    def __enter__(self):
        self.sandbox = Sandbox.create(template="code-interpreter")
        return self
        
    def __exit__(self, *args):
        if self.sandbox:
            self.sandbox.kill()
    
    def chat(self, message: str) -> str:
        """Send message and get response with code execution"""
        self.conversation.append({
            "role": "user",
            "content": message
        })
        
        tools = [{
            "type": "function",
            "function": {
                "name": "execute_python",
                "description": "Execute Python code with NumPy, Pandas, Matplotlib",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "code": {"type": "string"}
                    },
                    "required": ["code"]
                }
            }
        }]
        
        response = self.client.chat.completions.create(
            model="gpt-4o",
            messages=self.conversation,
            tools=tools
        )
        
        response_message = response.choices[0].message
        self.conversation.append(response_message)
        
        # Execute code if tool called
        if response_message.tool_calls:
            for tool_call in response_message.tool_calls:
                if tool_call.function.name == "execute_python":
                    code = json.loads(tool_call.function.arguments)['code']
                    execution = self.sandbox.run_code(code)
                    
                    self.conversation.append({
                        "role": "tool",
                        "name": "execute_python",
                        "content": execution.stdout or execution.stderr,
                        "tool_call_id": tool_call.id
                    })
            
            # Get final response
            final = self.client.chat.completions.create(
                model="gpt-4o",
                messages=self.conversation
            )
            return final.choices[0].message.content
        
        return response_message.content

# Usage
with CodeInterpreter(api_key="sk-...") as interpreter:
    print(interpreter.chat("Analyze this data: [100, 150, 120, 180, 200]"))
    print(interpreter.chat("Now create a bar chart"))
    print(interpreter.chat("What's the average?"))

Next Steps