💡 Async Version: This documentation covers the synchronous API. For async/await support, see
AsyncAgentBaywhich provides the same functionality with async methods.
- First Session Tutorial - Get started with creating your first AgentBay session
class AgentBay()AgentBay represents the main client for interacting with the AgentBay cloud runtime environment asynchronously.
def __init__(self, api_key: str = "",
cfg: Optional[Config] = None,
env_file: Optional[str] = None)Initialize AgentBay client.
Arguments:
api_key: API key for authentication. If not provided, will read from AGENTBAY_API_KEY environment variable.
cfg: Configuration object. If not provided, will load from environment variables and .env file.
env_file: Custom path to .env file. If not provided, will search upward from current directory.
def create(params: Optional[CreateSessionParams] = None) -> SessionResultCreate a new session in the AgentBay cloud environment asynchronously.
Arguments:
paramsOptional[CreateSessionParams], optional - Parameters for creating the session. Defaults to None (uses default configuration).
Returns:
SessionResult: Result containing the created session and request ID.
- success (bool): True if the operation succeeded
- session (Session): The created session object (if success is True)
- request_id (str): Unique identifier for this API request
- error_message (str): Error description (if success is False)
Raises:
ValueError: If API key is not provided and AGENTBAY_API_KEY environment variable is not set.
ClientException: If the API request fails due to network or authentication issues.
Example:
result = agent_bay.create()
session = result.session
info_result = session.info()
print(f"Session ID: {info_result.session_id}")
session.delete()def list(labels: Optional[Dict[str, str]] = None,
page: Optional[int] = None,
limit: Optional[int] = None,
status: Optional[str] = None) -> SessionListResultReturns paginated list of session IDs filtered by labels asynchronously.
Arguments:
labelsOptional[Dict[str, str]], optional - Labels to filter sessions. Defaults to None (returns all sessions).pageOptional[int], optional - Page number for pagination (starting from 1). Defaults to None (returns first page).limitOptional[int], optional - Maximum number of items per page. Defaults to None (uses default of 10).statusOptional[str], optional - Status to filter sessions. Must be one of: RUNNING, PAUSING, PAUSED, RESUMING, DELETING, DELETED. Defaults to None (returns sessions with any status).
Returns:
SessionListResult: Paginated list of session IDs that match the filters.
def delete(session: Session, sync_context: bool = False) -> DeleteResultDelete a session by session object asynchronously.
Arguments:
sessionSession - The session to delete.sync_contextbool - Whether to sync context data (trigger file uploads) before deleting the session. Defaults to False.
Returns:
DeleteResult: Result indicating success or failure and request ID.
def get(session_id: str) -> SessionResultGet a session by its ID asynchronously.
Arguments:
session_idstr - The ID of the session to retrieve. Must be a non-empty string.
Returns:
SessionResult: Result containing the Session instance, request ID, and success status.
def beta_pause(session: Session,
timeout: int = 600,
poll_interval: float = 2.0) -> SessionPauseResultAsynchronously pause a session (beta), putting it into a dormant state.
def beta_pause_async(session: Session) -> SessionPauseResultFire-and-return pause: trigger PauseSessionAsync without waiting for PAUSED.
This method directly calls the PauseSessionAsync API without waiting for the session to reach the PAUSED state. For behavior that waits for the PAUSED state, use the pause() method instead.
Arguments:
sessionSession - The session to pause.
Returns:
SessionPauseResult: Result containing the request ID and success status.
def beta_resume(session: Session,
timeout: int = 600,
poll_interval: float = 2.0) -> SessionResumeResultAsynchronously resume a session (beta) from a paused state.
def beta_resume_async(session: Session) -> SessionResumeResultFire-and-return resume: trigger ResumeSessionAsync without waiting for RUNNING.
This method directly calls the ResumeSessionAsync API without waiting for the session to reach the RUNNING state. For behavior that waits for the RUNNING state, use the resume() method instead.
Arguments:
sessionSession - The session to resume.
Returns:
SessionResumeResult: Result containing the request ID and success status.
Related APIs:
Documentation generated automatically from source code using pydoc-markdown.