Principle:Langchain ai Langgraph Language Model Configuration
| Attribute | Value |
|---|---|
| Concept | Configuring and binding a language model with tools for agent use |
| Workflow | ReAct_Agent_Creation |
| Type | Principle |
| Repository | Langchain_ai_Langgraph |
| Source | libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py (L563-589)
|
Overview
Language model configuration in the ReAct agent workflow encompasses the process of selecting, initializing, and preparing a chat model so that it can participate in tool-calling loops. This involves three key operations: model initialization (converting a string identifier like "openai:gpt-4" into a BaseChatModel instance), tool binding (informing the model about available tools via bind_tools), and prompt composition (prepending system instructions to the model input). LangGraph supports both static model configuration (a single model used throughout execution) and dynamic model selection (a callable that returns different models based on runtime state and context).
Description
LLM Tool-Calling
Modern chat models support tool calling (also known as function calling) as a native capability. When tools are bound to a model via bind_tools, the model receives JSON schemas describing each tool's name, description, and parameter types. During inference, the model can choose to emit structured tool_calls in its response rather than (or in addition to) text content.
The tool-calling protocol in LangGraph operates as follows:
- Tools are bound to the model, which modifies the model's configuration to include tool schemas.
- The model receives the message history and produces an
AIMessage. - If the
AIMessagecontainstool_calls, the agent routes to tool execution. - Tool results are appended as
ToolMessageobjects and the model is called again.
This binding step is critical because it establishes the contract between the model and the available tools. Without binding, the model has no knowledge of what tools exist or how to invoke them.
Model Binding
The bind_tools method on BaseChatModel accepts a list of tool definitions (as BaseTool instances, function schemas, or dictionaries) and returns a new model instance that includes those tool schemas in every request. In the context of create_react_agent, tool binding happens automatically during agent construction:
- If the model is a string (e.g.,
"anthropic:claude-3-7-sonnet-latest"),init_chat_modelis called to produce aBaseChatModel. - The function checks whether the model already has tools bound (via
_should_bind_tools). - If tools need binding,
model.bind_tools(tool_classes + builtin_tools)is called. - The resulting bound model is composed with the prompt runnable via the
|(pipe) operator.
This automatic binding means users typically do not need to call bind_tools explicitly when using create_react_agent -- they simply pass the model and tools as separate arguments.
Dynamic Model Selection
LangGraph supports dynamic model selection where instead of a fixed model instance, users provide a callable with the signature (state, runtime) -> BaseChatModel. This enables:
- Context-dependent model routing: Using a cheaper model for simple queries and a more capable model for complex ones.
- Runtime configuration: Selecting models based on user preferences stored in runtime context.
- A/B testing: Switching between model versions based on experimental flags.
When a dynamic model is used, tool binding must be performed inside the callable because the model instance is not known at graph construction time. The callable receives the full graph state and a Runtime object providing access to context, store, and other runtime services.
Prompt Composition
The prompt is composed with the model via LangChain's pipe operator (|), creating a RunnableSequence that first applies the prompt transformation and then invokes the model. The prompt can be:
- A string: Converted to a
SystemMessageprepended to the message list. - A SystemMessage: Directly prepended to messages.
- A Callable: A function that receives the full state and returns transformed input for the model.
- A Runnable: A LangChain runnable that processes the state before model invocation.
The composed chain prompt | model is stored as static_model for static configurations or constructed at runtime for dynamic models.
Usage
from langgraph.prebuilt import create_react_agent
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get weather for a city."""
return f"Sunny in {city}"
# String model identifier - automatically initializes and binds tools
agent = create_react_agent("openai:gpt-4", tools=[get_weather])
# With explicit model instance
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4")
agent = create_react_agent(model, tools=[get_weather])
# Dynamic model selection
from langgraph.runtime import Runtime
def select_model(state, runtime: Runtime):
if runtime.context.get("use_advanced"):
model = ChatOpenAI(model="gpt-4")
else:
model = ChatOpenAI(model="gpt-3.5-turbo")
return model.bind_tools([get_weather])
agent = create_react_agent(select_model, tools=[get_weather])
Theoretical Basis
The language model configuration pattern draws from the adapter pattern in software engineering, where bind_tools adapts a general-purpose language model into a tool-aware agent component. The model itself remains stateless -- all tool awareness is achieved through schema injection at the API level rather than model modification.
The dynamic model selection pattern embodies the strategy pattern, allowing the model selection algorithm to vary independently from the agent architecture. This separation enables runtime optimization decisions without altering the graph topology.
The prompt composition via the pipe operator implements a chain of responsibility, where each stage (prompt transformation, then model invocation) processes and forwards the input. This functional composition approach, native to LangChain's Runnable protocol, ensures that each component remains independently testable and reusable.