This topic describes the core concepts you need to understand to use the AgentBay SDK.
AgentBay Class
Core Functions
The AgentBay class is the main interface for interacting with Alibaba Cloud services. It provides these core functions:
Session Manager: Create, delete, and manage cloud sessions.
API Client: Handle all communication with the AgentBay cloud service.
Authentication Handler: Automatically manage API keys and security.
Basic Usage
# 1. Initialize client
agent_bay = AgentBay()
# 2. Create session (uses linux_latest by default)
session = agent_bay.create().session
# 3. Use session for your tasks
# ... your automation tasks ...
# 4. Clean up resources
agent_bay.delete(session)
Sessions
A session is a connection between a user and a cloud environment.
Key Features
Temporary: Sessions are created when needed and destroyed after completion.
Isolated: Each session is fully independent from other sessions.
Billed: You pay for the time a session remains active.
Basic Usage
# Create a session
session = agent_bay.create().session
# Use the session for your tasks
session.command.execute_command("echo 'Hello World'")
# Always clean up when done
agent_bay.delete(session)
Session Lifecycle
Create Session → Use Session → Delete Session
↓ ↓ ↓
Allocate Execute Release
Resources Operations ResourcesSession Release
You must release a session after it finishes to free cloud resources. There are two ways to do this:
Manual Release (Recommended)
# Explicitly delete when done
agent_bay.delete(session)
Automatic Timeout Release
If you do not delete a session manually, it releases automatically after timing out.
Go to the Alibaba Cloud AgentBay Console. In the left navigation pane, choose Policy Management.
Click Create Policy. Set Release inactive desktops to Enable.
Enter timeout values for Release desktop after MCP interaction ends and Release desktop after user interaction ends.
Click Create Policy.
In the left navigation pane, click Service Management. Locate the API key. In the Actions column, click the ⋮ icon.
You can choose View/Associate Policy. In the Associated Policies section, click Associate Policy. Select the policy that you created. Click Confirm Association.
NoteEach API key can be associated with only one policy. When you create an API key, it is automatically associated with the default policy. You must first click Dissociate.
Images
Official OS Images
The table below lists the latest official OS images provided by AgentBay.
Image ID | Environment | Best For |
| Cloud PC | General computing and server tasks (default if unspecified) |
| Cloud PC | General Windows tasks, |
| Cloud Browser | Web scraping, browser automation, and website testing |
| Code Sandbox | Coding, development tools, and programming tasks |
| Cloud Phone | Mobile app testing and Android automation |
If you do not specify an
image_id, AgentBay useslinux_latestas the default environment.You can create and use custom images in the AgentBay console to meet specific needs.
Selecting the Right Image
Windows Environment Example
from agentbay.session_params import CreateSessionParams
# Create Windows environment and automate notepad
params = CreateSessionParams(image_id="windows_latest")
session = agent_bay.create(params).session
# Start Notepad application
session.computer.start_app("notepad.exe")
# Returns: ProcessListResult with started process info
# Input text into notepad
session.computer.input_text("Hello from Windows!")
# Returns: BoolResult with success status
agent_bay.delete(session)
Browser Environment Example
# Create browser environment
params = CreateSessionParams(image_id="browser_latest")
session = agent_bay.create(params).session
# Initialize and navigate
from agentbay.browser import BrowserOption
session.browser.initialize(BrowserOption())
session.browser.agent.navigate("https://www.baidu.com")
print("Web navigation successful")
agent_bay.delete(session)Code Sandbox Environment Example
# Create development environment and execute code
params = CreateSessionParams(image_id="code_latest")
session = agent_bay.create(params).session
# Execute code
result = session.code.run_code("print('Hello from CodeSpace!')", "python")
# Returns: CodeExecutionResult with output
# Example: result.result = "Hello from CodeSpace!"
agent_bay.delete(session)Cloud Phone Environment Example
# Create Android environment and send HOME key
params = CreateSessionParams(image_id="mobile_latest")
session = agent_bay.create(params).session
# Press HOME key to return to home screen
from agentbay.mobile import KeyCode
session.mobile.send_key(KeyCode.HOME)
# Returns: BoolResult with success status
# Example: result.success = True (returns to Android home screen)
agent_bay.delete(session)
Data Persistence
Temporary Data
By default, all data in a session is temporary.
All data is lost when the session ends.
Use this for task processing, temporary files, or caching.
# This data will be LOST when session ends
session.file_system.write_file("/tmp/temp_data.txt", "This will disappear")
Persistent Data
Supports retaining data across sessions.
You must configure it explicitly.
Use this for project files, configurations, and important results.
from agentbay import ContextSync
# Create persistent storage
context = agent_bay.context.get("my-project", create=True).context
context_sync = ContextSync.new(context.id, "/tmp/persistent")
# Create session with persistent data
params = CreateSessionParams(context_syncs=[context_sync])
session = agent_bay.create(params).session
# This data will be SAVED across sessions
session.file_system.write_file("/tmp/persistent/important.txt", "This will persist")To save data, you must use a Context. Otherwise, data is permanently lost when the session ends.
API Results and Request IDs
API Results
When you call an AgentBay API, the result is wrapped in a result object.
# Example API call
screenshot = session.computer.screenshot()
# The result object contains:
print(screenshot.success) # True/False - whether the operation succeeded
print(screenshot.data) # Your actual data (screenshot URL)
print(screenshot.request_id) # Request ID for troubleshooting
Request ID
Every API call to AgentBay returns a unique request ID. It looks like "ABC12345-XXXX-YYYY-ZZZZ-123456789ABC".
Uses for Request IDs
Troubleshooting: If something goes wrong, share this ID to obtain faster help.
Tracing: Helps track individual operations in trace logs.
Debugging: Makes it easier to identify which specific API call failed.
When to Use Request IDs
An API call fails unexpectedly.
You notice performance issues with a specific operation.
You contact helpdesk about an issue.
Troubleshooting Example
result = session.code.run_code("print('hello')", "python")
if not result.success:
print(f"Code execution failed! Request ID: {result.request_id}")
# Share this Request ID with support for faster help