All Products
Search
Document Center

AgentBay:Key concepts

Last Updated:Jun 22, 2026

Learn the key concepts of the AgentBay SDK, including sessions, images, data persistence, and API results.

AgentBay Class

Core Functions

The AgentBay class is the primary interface for interacting with the AgentBay service. It provides the following core functions:

  • Session manager: Creates, deletes, and manages cloud sessions.

  • API client: Handles all communication with the AgentBay cloud service.

  • Authentication handler: Manages API keys and security automatically.

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 represents 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      Resources

Session release

You must release a session after use 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 is released automatically after timing out.

  1. Go to the AgentBay console. In the left navigation pane, choose Policy Management.

  2. Click Create Policy. Set Release Inactive Desktops to Enable.

  3. Enter timeout values for Release Desktop after MCP Interaction Terminates and Release Desktop after MCP Interaction Terminates.

  4. Click Create Policy.

  5. In the left-side navigation pane, choose Service Management. Locate the API key. In the Actions column, click the ⋮ icon.

  6. Choose View/Associate Policy. In the Associated Policy section, click Associate Policy. Select the policy that you created. Click Confirm.

    Note

    Each 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 system images

The following table lists the latest official system images provided by AgentBay.

Image ID

Environment

Best For

linux_latest

Cloud computer

General computing and server tasks (default if unspecified)

windows_latest

Cloud computer

General Windows tasks, .NET development, and Windows applications

browser_latest

Cloud browser

Web scraping, browser automation, and website testing

code_latest

Code sandbox

Coding, development tools, and programming tasks

mobile_latest

Cloud Phone

Mobile app testing and Android automation

Note
  • If you do not specify an image_id, AgentBay uses linux_latest as the default environment.

  • You can create and use custom images in the AgentBay console to meet specific needs.

Select an appropriate 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

  • Data in a session is retained across sessions when you use persistent storage.

  • You must configure persistent storage 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")
Note

To persist data, you must use a Context. Otherwise, data is permanently lost when the session ends.

API results and request IDs

API results

AgentBay API calls return results 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 returns a unique request ID, such as "ABC12345-XXXX-YYYY-ZZZZ-123456789ABC".

Uses for request IDs
  • Troubleshooting: Share this ID with support to get faster help.

  • Tracing: Track individual operations in trace logs.

  • Debugging: 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 support 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