Skip to main content
MobileAgent - A wrapper class that coordinates the planning and execution of tasks to achieve a user’s goal on an Android or iOS device.

MobileAgent

A wrapper class that coordinates between agents to achieve a user’s goal. Architecture:
  • When reasoning=False: Uses FastAgent directly for immediate execution
  • When reasoning=True: Uses ManagerAgent (planning) + ExecutorAgent (actions)

MobileAgent.__init__

Initialize the MobileAgent wrapper. Arguments:
  • goal str - User’s goal or command to execute
  • config MobileConfig | None - Full configuration object (required if llms not provided). Contains agent settings, LLM profiles, device config, and more.
  • llms dict[str, LLM] | LLM | None - Optional LLM configuration:
    • dict[str, LLM]: Agent-specific LLMs with keys: “manager”, “executor”, “fast_agent”, “app_opener”, “structured_output”
    • LLM: Single LLM instance used for all agents
    • None: LLMs will be loaded from config.llm_profiles
  • custom_tools dict - Custom tool definitions. Format: {"tool_name": {"parameters": {...}, "description": "...", "function": callable}}. These are merged with auto-generated credential tools.
  • credentials Union[dict, CredentialManager, None] - Direct credential mapping {"SECRET_ID": "value"}, a CredentialManager instance, or None. If None, credentials will be loaded from config.credentials if available.
  • variables dict | None - Custom variables accessible throughout execution. Available in shared_state.custom_variables.
  • output_model Type[BaseModel] | None - Pydantic model for structured output extraction from final answer. If provided, the final answer will be parsed into this model.
  • prompts dict[str, str] | None - Custom Jinja2 prompt templates to override defaults. Keys: “fast_agent_system”, “fast_agent_user”, “manager_system”, “executor_system”. Values: Jinja2 template strings (NOT file paths).
  • driver DeviceDriver | None - Pre-configured device driver instance (AndroidDriver or IOSDriver). If None, a driver will be created from config.
  • state_provider StateProvider | None - Pre-configured state provider instance. If None, a state provider will be created from config.
  • timeout int - Workflow timeout in seconds (default: 1000)
Basic initialization pattern (recommended):
Loading from YAML (optional):
Custom LLM dictionary pattern:
Single LLM pattern:
Custom tools and credentials:
Structured output extraction:

MobileAgent.run

Run the MobileAgent workflow. Returns:
  • ResultEvent - Result object with the following attributes:
    • success (bool): True if task completed successfully
    • reason (str): Success message or failure reason
    • steps (int): Number of steps executed
    • structured_output (Any): Parsed Pydantic model (if output_model provided, otherwise None)
Usage:
Streaming events:

MobileAgent.send_user_message

Inject an external user message into the running workflow. The message is queued and consumed by the active agent (FastAgent or Manager) at its next step. Arguments:
  • message str - The message to inject
Returns:
  • QueuedUserMessage - Object with id, message, and queued_at_step fields
Usage:
If the agent has already finished or the message arrives at max_steps, the message will be dropped and an ExternalUserMessageDroppedEvent is emitted. Pending messages also block complete() until they are consumed.

Event Types

MobileAgent emits various events during execution: Workflow Events:
  • StartEvent - Workflow started
  • ManagerInputEvent - Manager planning phase started
  • ManagerContextEvent - Manager received context for planning
  • ManagerResponseEvent - Manager intermediate response
  • ManagerPlanEvent - Manager created a plan
  • ManagerPlanDetailsEvent - Manager plan details
  • ExecutorInputEvent - Executor action phase started
  • ExecutorContextEvent - Executor received context
  • ExecutorResponseEvent - Executor intermediate response
  • ExecutorActionEvent - Executor action details
  • ExecutorActionResultEvent - Executor action result details
  • ExecutorResultEvent - Executor completed an action
  • ExternalUserMessageAppliedEvent - External message consumed by agent
  • ExternalUserMessageDroppedEvent - External message dropped (e.g., at max steps)
  • FastAgentExecuteEvent - FastAgent started (direct mode)
  • FastAgentResultEvent - FastAgent completed
  • FinalizeEvent - Workflow finalizing
  • StopEvent - Workflow completed
Common Events:
  • ToolExecutionEvent - Emitted by ToolRegistry after every tool dispatch (contains tool_name, tool_args, success, summary)
  • ScreenshotEvent - Screenshot captured
  • RecordUIStateEvent - UI state recorded

Configuration

MobileAgent uses a hierarchical configuration system. See the Configuration Guide for details. Key configuration options:

Advanced Usage

Custom Tools instance:
Custom variables:
Variables are accessible in shared_state.custom_variables throughout execution and can be referenced in custom tools or scripts. Custom prompts:
Available prompt keys: “fast_agent_system”, “fast_agent_user”, “manager_system”, “executor_system”

Notes

  • Config requirement: Either config or llms must be provided. If llms is not provided, config is required to load LLMs from profiles.
  • Vision mode: Enabling vision (agent_config.*.vision = True) increases token usage as screenshots are sent to the LLM.
  • Reasoning mode: reasoning=True uses Manager/Executor workflow for complex planning. reasoning=False uses FastAgent for direct execution.
  • Timeout: Default is 1000 seconds. Increase for long-running tasks.
  • Credentials: When credentials are provided, the type_secret(secret_id, index) tool is automatically registered. The agent never sees the actual secret values, only the secret IDs.