Skip to main content
A ready-to-run example is available here!
The built-in route_task_to_model tool (ClassifyAndSwitchLLMTool) lets an agent switch models mid-conversation based on what the current task looks like. When the agent calls the tool, a lightweight classifier LLM inspects the recent conversation, picks the most suitable saved LLM profile, and switches the conversation to that profile before the agent continues. Conversation history and combined usage metrics are preserved across the switch. This differs from Model Routing, where a Router decides per request based on fixed rules. Here the decision is made by an LLM, and the agent decides when to ask for it.

How It Works

  1. Save the candidate LLM profiles. The tool switches profiles by name, so every model it can choose must exist in the LLMProfileStore, or be supplied inline (see below).
  2. Define a meta-profile. A MetaProfile names the classifier profile and describes how to map a task to a target profile.
  3. Enable the tool on the agent. Set enable_classify_and_switch_llm_tool=True on OpenHandsAgentSettings and point active_meta_profile (or meta_profile) at the routing configuration.
  4. Let the agent call it. The agent starts on its configured LLM and switches only when it invokes route_task_to_model.

Meta-Profile Shapes

A MetaProfile supports two mutually exclusive routing modes.

Structured classes

Provide a fixed set of task categories. The classifier is shown the categories and returns the number of the best match; the tool switches to that class’s model, which is a saved profile name.

Direct prompt

Provide a free-form prompt_template instead of classes. The template must contain {{ instance_text }} (the recent conversation) and may contain {{ model_table }} (the model_table text). The classifier is expected to return JSON with a model field naming a saved profile:
The returned name is matched case-insensitively against saved profile names, and the canonical profile name is used for the switch.

Where Meta-Profiles Come From

Meta-profiles are stored by name in ~/.openhands/meta-profiles and managed with MetaProfileStore. The tool resolves its configuration lazily, at invocation time, in this order:
  1. If active_meta_profile is set, the store is authoritative and the meta-profile is loaded by name.
  2. If the store cannot resolve that name, or no name is set, the inline meta_profile from the agent settings is used. Cloud runtimes rely on this because their ephemeral filesystem has no meta-profile store.
  3. If neither applies, the alphabetically first meta-profile in the store is used.
Target and classifier profiles are likewise loaded from the LLMProfileStore, or from the meta_profile_llms map on the settings when supplied inline.
If the classifier returns no usable answer, or names a profile that does not exist, the tool returns an error observation instead of silently routing to a default model. The agent sees the failure and can retry.

Enabling the Tool

Wire everything through OpenHandsAgentSettings:
create_agent() defaults the agent’s toolset to terminal, file_editor, and task_tracker by name. Their implementations live in openhands-tools and are registered only when imported, so call register_default_tools() (or import the tool modules you need) before creating the agent. Passing tools=[] instead builds a bare agent that has only the built-in tools plus route_task_to_model.

Ready-to-run Example

This example is available on GitHub: examples/01_standalone_sdk/59_route_task_to_model.py
Save a set of profiles, define a structured meta-profile, enable the tool, and ask the agent to route the task:
examples/01_standalone_sdk/59_route_task_to_model.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Next Steps