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: 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. Agent Skills are not part of system_prompt. Agent.call_llm adds the selected skills to each request before it delegates to call(). See SkillsManager. 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. The core settings include:
  • temperature from the agent. It defaults to None, and LiteLLM leaves it out of the request when unset, so models that reject any temperature value, such as some Claude models, work without extra configuration.
  • usage_hook, set to the agent’s own usage counter. Token totals live on the agent, so they survive a rebuild after a fallback switch. See Token usage.
  • base_url and api_key, from the agent’s llm_base_url and llm_api_key.
MCP tool schemas come from the agent’s ToolManager. With dynamic tool loading on, they are deferred into the tool catalog (agent.tool_manager.defer_mcp_tools()). Otherwise they are fetched with agent.tool_manager.add_mcp_tools_to_memory() and sent with every request.
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.
List[str]
Multiple image inputs for multimodal models, same accepted forms as img.
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. Any provider error is logged and re-raised. The agent’s standard loop (Agent._run) retries the call up to retry_attempts times. After the last failure it raises AgentLLMError, which hands the task to the fallback chain below.

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. Agent.run calls it when a run fails and more than one model is configured. When every model is exhausted, the original error goes to the agent’s error handler, which logs it (and saves state when autosave is on) and re-raises it to the caller.

Agent-level wrappers

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

Agent

The class that owns the manager

Model Providers

Supported models and provider configuration