Prerequisites
- GravixLayer SDK installed (
pip install gravixlayer) - API Key configured
Example: Python Code Interpreter
This example shows a simple loop where an LLM generates Python code to solve a problem, and the Sandbox executes it.- Python
- JavaScript
Copy
# 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-8b-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:
{code}
")
# 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'])
Copy
// npm install gravixlayer
import { GravixLayer, Sandbox } from 'gravixlayer';
// Create Gravix client
const client = new GravixLayer();
const 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.";
const prompt = "Calculate how many r's are in the word 'strawberry'";
// Send messages to Gravix Inference API
const response = await client.chat.completions.create({
model: "meta-llama/llama-3.1-8b-instruct",
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt }
]
});
// Extract the code from the response
const llmContent = response.choices[0].message.content;
// Clean up code block formatting (strip backticks)
const code = llmContent.replace(/```python/g, "").replace(/```/g, "").trim();
console.log(`Generated Code:
${code}
`);
// Execute code in Gravix AgentBox
if (code) {
// Create a sandbox
const sandbox = await Sandbox.create();
console.log(`Created Sandbox: ${sandbox.sandbox_id}`);
try {
// Run the code
const execution = await sandbox.runCode(code);
// Print results
if (execution.logs.stdout && execution.logs.stdout.length > 0) {
console.log("Output:", execution.logs.stdout.join('\n'));
}
if (execution.logs.stderr && execution.logs.stderr.length > 0) {
console.log("Error:", execution.logs.stderr.join('\n'));
}
} finally {
// Cleanup
await sandbox.kill();
console.log("Sandbox deleted");
}
}
Related Resources
- Sandbox Management
- Code Execution
- Chat Completions

