Skip to main content
AgentBox is the perfect companion for Large Language Models (LLMs), enabling them to execute code, analyze data, and interact with the real world. By combining LLMs with secure sandboxes, you can build “Code Interpreter” style agents that can solve complex problems.

How it Works

The integration typically follows a Generate-Execute-Observe loop:
  1. Generate: The LLM generates code (Python, JavaScript, etc.) based on a user query or task.
  2. Execute: The code is sent to an AgentBox sandbox for secure execution.
  3. Observe: The output (stdout, stderr, files) is captured and fed back to the LLM.
  4. Iterate: The LLM analyzes the output and decides whether to generate more code or provide a final answer.

Use Cases

  • Data Analysis Agents: Upload a CSV, ask the LLM to “analyze the trends,” and have it write Pandas code to generate insights and charts.
  • Coding Assistants: Verify generated code by running it in a real environment before showing it to the user.
  • Math & Logic Solvers: Offload complex calculations to Python instead of relying on the LLM’s internal arithmetic.
  • File Processing: Generate scripts to convert, resize, or modify files uploaded to the sandbox.

Implementation Example

# pip install gravixlayer
from gravixlayer import GravixLayer, Sandbox
import re

# Create Gravix client
client = GravixLayer()

system = "You are a helpful assistant that can execute python code. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
prompt = "Calculate how many r's are in the word 'strawberry'"

# Send messages to Gravix Inference API
response = client.chat.completions.create(
    model="meta-llama/llama-3.1-70b-instruct",
    messages=[
        {"role": "system", "content": system},
        {"role": "user", "content": prompt}
    ]
)

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

# Clean up code block formatting (strip backticks)
code = llm_content.replace("```python", "").replace("```", "").strip()

print(f"Generated Code:\n{code}\n")

# Execute code in Gravix AgentBox
if code:
    # Create a sandbox
    sandbox = Sandbox.create()
    print(f"Created Sandbox: {sandbox.sandbox_id}")
    
    # Run the code
    execution = sandbox.run_code(code)
    
    # Print results
    if execution.logs['stdout']:
        print("Output:", execution.logs['stdout'])
    if execution.logs['stderr']:
        print("Error:", execution.logs['stderr'])

Example Workflow

Here is a conceptual workflow for a Data Analysis Agent:
  1. Upload Data: User uploads sales.csv to the sandbox using the File API.
  2. Prompt LLM: “Read sales.csv and plot the monthly revenue.”
  3. LLM Generates Code:
    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.read_csv('sales.csv')
    # ... plotting code ...
    plt.savefig('revenue.png')
    print("Plot saved to revenue.png")
    
  4. Execute in Sandbox: Run the code using the Code Execution API.
  5. Retrieve Result: Download revenue.png from the sandbox and display it to the user.

Next Steps

  • Check out the SDK Guide for code examples.
  • See the Cookbooks for end-to-end implementations.