Skip to main content

Overview

LLMManager owns the language-model plumbing of an Agent: building the LiteLLM instance from configuration, checking model capabilities, invoking the model across all streaming modes, and turning the raw stream into reasoning, content and tool-call parts. Fallback between models is not here: Agent.run walks fallback_models itself (see Model fallback). 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. The llm the agent builds from the manager is stored on the agent, so agent.llm, serialization, and save() / load() all work against it.
Any
required
The owning Agent. Read for configuration. build() sets agent.model_name to gpt-5.4 when it is None.

Model fallback

Fallback runs in one flat loop in Agent.run, not in the manager. Configure it on the agent:
  • Retries belong to the client. retry_attempts is passed to LiteLLM as retries and sent with every request as litellm’s num_retries, so one model gets at most retry_attempts + 1 calls.
  • The first entry of fallback_models is the primary, and agent.model_name is set to it. fallback_model_name is folded into fallback_models (as [model_name, fallback_model_name]) at construction.
  • Only an AgentLLMError moves to the next model. Agent.call_llm wraps any error from call() in AgentLLMError, chained to the provider’s error as __cause__. Any other exception, such as a bug in a tool, raises at once. After the last model fails, the AgentLLMError reaches the caller.
  • Every run starts from the primary model again, rebuilding agent.llm with llm_handling() whenever the model changes.
The fallback-index helpers were removed: get_available_models, get_current_model, switch_to_next_model, reset_model_index, is_fallback_available, current_model_index, and LLMManager.handle_fallback_execution, on both LLMManager and Agent. Calling one raises AttributeError. Read agent.fallback_models for the configured chain and agent.model_name for the model in use.

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.
  • retries, from the agent’s retry_attempts.
  • stop, set to [agent.stopping_token] when the agent has a stopping_token, so the provider stops generating there. llm_args can override it.
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 each capability the current model lacks: an error for vision when an image is supplied, an error for function calling when a tool schema is set, and a warning for parallel function calling when more than two tools are registered. The function-calling messages are logged once per process. 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. Every streaming mode reads from stream_parts, so reasoning and tool calls stream along with the content.
Direct llm.run() returning the complete string. Used when stream and streaming_on are off and no streaming_callback is given.
Each call runs inside an Agent.llm_call telemetry span that records the request, the response and the tokens used.
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, and the text of every streamed part in panel streaming.
Returns the complete response string (content only, without reasoning or tool-call text), 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 propagates. litellm has already retried it retry_attempts times; Agent.call_llm then wraps it in AgentLLMError, which moves Agent.run to the next model in fallback_models. See Model fallback.

run_side_call

Run a call on a model other than the agent’s own, such as the tool-call summary model, and stream it the same way call() would when the agent streams. streaming_callback defaults to the callback of the run in progress, then to the agent’s streaming_callback. Returns the model’s reply.

Stream plumbing

stream_parts

Yield everything the model streams as it arrives, as (kind, text, chunk) tuples where kind is "reasoning", "content" or "tool_call".
  • Content passes through unchanged and is appended to content_out; joined, it is the reply text.
  • Reasoning starts with a [thinking] header.
  • Each tool call starts with a [tool call] <name> header, followed by a space and its argument fragments.
A part of a new kind starts on a new line, and a trailing newline ends a stream that finished on reasoning or a tool call. tool_calls_out is filled with the assembled tool calls once the stream ends. In max_loops="auto" almost every turn is a tool call, so a callback sees the whole autonomous run:
To keep only the reply text, filter callback text on the headers, or use detailed streaming and filter on token_info["type"].

stream_with_tool_collection

Forward every chunk unchanged while assembling fragmented delta.tool_calls deltas into a complete tool-call list, which fills tool_calls_out once the stream is consumed. stream_parts reads through it.

extract_thinking_from_stream

Swallow reasoning chunks, flush them to a thinking panel when print_on is set, and yield only content chunks onward. The streaming modes of call() no longer use it; they stream reasoning through stream_parts instead. It remains available through Agent._extract_thinking_from_stream.

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