This topic describes code execution in the AgentBay SDK CodeSpace environment. CodeSpace is a dedicated developer environment optimized for running Python and JavaScript code.
Overview
CodeSpace is the developer environment for AgentBay. It provides the following features:
Multi-language support: Run Python and JavaScript/Node.js code.
Isolated execution: Secure, containerized code execution.
Developer tools: Pre-installed interpreters and developer tools.
File operations: Read and write files to execute scripts.
CodeSpace environment
Create a CodeSpace session
To use the code execution feature, create a session with the code_latest image:
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
agent_bay = AgentBay(api_key="your-api-key")
session_params = CreateSessionParams(image_id="code_latest")
result = agent_bay.create(session_params)
if result.success:
session = result.session
print(f"CodeSpace session created: {session.session_id}")
else:
print(f"Session creation failed: {result.error_message}")Python code execution
Basic Python execution
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
agent_bay = AgentBay(api_key="your-api-key")
session_params = CreateSessionParams(image_id="code_latest")
result = agent_bay.create(session_params)
if result.success:
session = result.session
code = """
import os
import sys
print(f"Python version: {sys.version}")
print(f"Current directory: {os.getcwd()}")
print("Hello from AgentBay!")
"""
result = session.code.run_code(code, "python")
if result.success:
print("Output:", result.result)
# Output: Python version: 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0]
# Current directory: /workspace
# Hello from AgentBay!
else:
print("Execution failed:", result.error_message)
agent_bay.delete(session)Python code with calculations
session_params = CreateSessionParams(image_id="code_latest")
result = agent_bay.create(session_params)
if result.success:
session = result.session
code = """
import math
# Calculate factorial
def factorial(n):
return math.factorial(n)
# Fibonacci sequence
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(f"Factorial of 10: {factorial(10)}")
print(f"10th Fibonacci number: {fibonacci(10)}")
# List comprehension
squares = [x**2 for x in range(1, 11)]
print(f"Squares: {squares}")
"""
result = session.code.run_code(code, "python")
if result.success:
print("Output:", result.result)
# Output: Factorial of 10: 3628800
# 10th Fibonacci number: 55
# Squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
agent_bay.delete(session)
JavaScript code execution
Node.js code execution
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
agent_bay = AgentBay(api_key="your-api-key")
session_params = CreateSessionParams(image_id="code_latest")
result = agent_bay.create(session_params)
if result.success:
session = result.session
js_code = """
const fs = require('fs');
const path = require('path');
console.log('Node.js version:', process.version);
console.log('Current directory:', process.cwd());
// Create a simple file
fs.writeFileSync('/tmp/hello.txt', 'Hello from Node.js!');
console.log('File created successfully');
"""
result = session.code.run_code(js_code, "javascript")
if result.success:
print("Output:", result.result)
# Output: Node.js version: v18.20.5
# Current directory: /workspace
# File created successfully
else:
print("Execution failed:", result.error_message)
agent_bay.delete(session)JavaScript code with data processing
session_params = CreateSessionParams(image_id="code_latest")
result = agent_bay.create(session_params)
if result.success:
session = result.session
js_code = """
// Array operations
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const sum = numbers.reduce((a, b) => a + b, 0);
const avg = sum / numbers.length;
const squares = numbers.map(x => x * x);
console.log('Numbers:', numbers);
console.log('Sum:', sum);
console.log('Average:', avg);
console.log('Squares:', squares);
// Object operations
const data = {
name: 'AgentBay',
version: '1.0',
features: ['Python', 'JavaScript', 'File I/O']
};
console.log('\\nData:', JSON.stringify(data, null, 2));
"""
result = session.code.run_code(js_code, "javascript")
if result.success:
print("Output:", result.result)
# Output: Numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Sum: 55
# Average: 5.5
# Squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#
# Data: {
# "name": "AgentBay",
# "version": "1.0",
# "features": [
# "Python",
# "JavaScript",
# "File I/O"
# ]
# }
agent_bay.delete(session)Code execution with file I/O
Write and execute a script
from agentbay import AgentBay
agent_bay = AgentBay(api_key="your-api-key")
result = agent_bay.create()
if result.success:
session = result.session
script_content = """
import json
import sys
data = {
'message': 'Hello from uploaded script',
'args': sys.argv[1:] if len(sys.argv) > 1 else []
}
with open('/tmp/output.json', 'w') as f:
json.dump(data, f, indent=2)
print(json.dumps(data, indent=2))
"""
write_result = session.file_system.write_file("/tmp/script.py", script_content)
if write_result.success:
print("Script uploaded successfully")
exec_result = session.command.execute_command("python3 /tmp/script.py arg1 arg2")
if exec_result.success:
print("Script output:", exec_result.output)
# Script output: {
# "message": "Hello from uploaded script",
# "args": [
# "arg1",
# "arg2"
# ]
# }
output_result = session.file_system.read_file("/tmp/output.json")
if output_result.success:
print("Output file content:", output_result.content)
# Output file content: {
# "message": "Hello from uploaded script",
# "args": [
# "arg1",
# "arg2"
# ]
# }
else:
print("Execution failed:", exec_result.error_message)
else:
print("Script write failed:", write_result.error_message)
agent_bay.delete(session)Multi-file projects
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
agent_bay = AgentBay(api_key="your-api-key")
session_params = CreateSessionParams(image_id="code_latest")
result = agent_bay.create(session_params)
if result.success:
session = result.session
session.file_system.create_directory("/workspace/myproject")
main_py = """
from utils import greet
if __name__ == "__main__":
print(greet("AgentBay"))
"""
utils_py = """
def greet(name):
return f"Hello, {name}!"
"""
session.file_system.write_file("/workspace/myproject/main.py", main_py)
session.file_system.write_file("/workspace/myproject/utils.py", utils_py)
result = session.command.execute_command("cd /workspace/myproject && python3 main.py")
if result.success:
print("Project output:", result.output)
# Project output: Hello, AgentBay!
else:
print("Execution failed:", result.error_message)
agent_bay.delete(session)Best practices
Set timeouts for long-running code
The run_code() method has a default timeout of 60 seconds.
Because of gateway limitations, each request cannot exceed 60 seconds.
# The default timeout is 60 seconds
result = session.code.run_code(code, "python")
# Specify a custom timeout of up to 60 seconds
result = session.code.run_code(code, "python", timeout_s=60)Use only standard libraries
CodeSpace comes pre-installed with standard libraries for Python and Node.js. For optimal performance and reliability, use only built-in modules, such as the following:
Python: os, sys, json, math, datetime, and re.
JavaScript: fs, path, os, and crypto.