Skip to main content

Overview

LLMManager owns everything an Agent does with a language model: building the LiteLLM instance from configuration, rotating through fallback models when one fails, checking model capabilities, invoking the model across all streaming modes, and re-running a failed task down the fallback chain. Every Agent builds one automatically as agent.llm_manager. You rarely construct it yourself — but it is where the behavior lives, and the agent’s LLM methods are thin wrappers over it.

Import

Design

Unlike the agent’s other collaborators, LLMManager holds a reference back to its owning agent and reads configuration live rather than snapshotting it. That is deliberate. Agents mutate their own LLM configuration at run time: system_prompt grows when skills load, tools_list_dictionary changes when tools are registered, streaming_on is toggled per call by run_stream. A snapshot taken at construction would silently go stale. Anything the manager writes — model_name, current_model_index, llm — is written back to the agent, so agent.llm, serialization, and save() / load() all behave exactly as before.
Any
required
The owning Agent. Read for configuration; written to for model_name, current_model_index, and llm.

Model selection and fallback

Configure fallbacks on the agent, and the manager handles rotation.

get_available_models

Models in preference order. Built from fallback_models when set, otherwise from model_name plus fallback_model_name (de-duplicated).

get_current_model

The model currently in use. Falls back to the first available model, then to gpt-5.4, if the index runs out of range.

switch_to_next_model

Advance to the next model in the list, update agent.model_name, and rebuild agent.llm against it. Returns False when the list is exhausted. Model switches are always logged.

reset_model_index

Return to the primary model and rebuild the LLM.

is_fallback_available

True when more than one model is configured.

Construction

build

Assemble the LiteLLM instance from every configuration source: the agent’s core settings, llm_args, tools_list_dictionary, MCP tool schemas, and anything passed here.
Any
A single dict is merged directly into the configuration; anything else is stored under additional_args.
Any
Merged into the LiteLLM configuration, taking precedence over defaults.
Returns the instance, or None if initialization raised AgentLLMInitializationError. parallel_tool_calls is enabled automatically when the agent has two or more tools or any MCP server configured.

get_parameters

The current LiteLLM instance’s attributes as a string.

check_model_supports_utilities

Log an error for each capability the current model lacks — vision when an image is supplied, function calling when a tool schema is set, parallel function calling when more than two tools are registered. Logging only; never raises.

randomize_temperature

Reset the LLM’s temperature to a random value in [0.0, 1.0]. Used between loops when dynamic_temperature_enabled is set; falls back to 0.5 when the LLM exposes no temperature.

Invocation

call

Call the model, selecting one of three modes from the agent’s configuration:
Direct llm.run() returning the complete string. Used when neither stream nor streaming_on is set.
str
required
The prompt to send.
str
Image input for multimodal models — file path, URL, data URI, or raw base64.
int
default:"0"
Loop iteration, used in streaming panel titles.
Callable[[str], None]
Receives token_info dicts in detailed streaming, token strings in panel streaming.
Returns the complete response string — or, when the model made tool calls mid-stream, the assembled tool-call list instead. is_last is stripped from kwargs before dispatch.

Stream plumbing

stream_with_tool_collection forwards every chunk unchanged while assembling fragmented delta.tool_calls deltas into a complete tool-call list. extract_thinking_from_stream swallows reasoning chunks from models that emit them, flushes them to a thinking panel, and yields only content chunks onward.

Fallback execution

handle_fallback_execution

Re-run a failed task against the next fallback model, recursing down the chain until it succeeds or every model is exhausted — at which point the original error goes to the agent’s error handler and None is returned.

Agent-level wrappers

Each of these delegates straight to the manager and remains the supported public API:

Agent

The class that owns the manager

Model Providers

Supported models and provider configuration