LangGraph : Prebuilt エージェント : モデル

このページはエージェントが使用するチャットモデルを構成する方法について説明します。

LangGraph : Prebuilt エージェント : モデル

作成 : クラスキャット・セールスインフォメーション
作成日時 : 06/09/2025

* 本記事は langchain-ai.github.io の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています :

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

LangGraph : Get started : Prebuilt エージェント : モデル

このページはエージェントが使用するチャットモデルを構成する方法について説明します。

 

ツール呼び出しのサポート

ツール呼び出しをするエージェントを有効にするには、基礎的な LLM が ツール呼び出し をサポートしている必要があります。

互換性のあるモデルは LangChain 統合ディレクトリ にあります。

 

モデルを名前で指定する

モデル名の文字列を使用してエージェントを設定できます :

OpenAI

import os
from langgraph.prebuilt import create_react_agent

os.environ["OPENAI_API_KEY"] = "sk-..."

agent = create_react_agent(
    model="openai:gpt-4.1",
    # other parameters
)

Anthropic

import os
from langgraph.prebuilt import create_react_agent

os.environ["ANTHROPIC_API_KEY"] = "sk-..."

agent = create_react_agent(
    model="anthropic:claude-3-7-sonnet-latest",
    # other parameters
)

 

init_chat_model の使用

init_chat_model ユーティリティは設定可能なパラメータを使用してモデルの初期化を単純化します :

OpenAI

pip install -U "langchain[openai]"
import os
from langchain.chat_models import init_chat_model

os.environ["OPENAI_API_KEY"] = "sk-..."

model = init_chat_model(
    "openai:gpt-4.1",
    temperature=0,
    # other parameters
)

Anthropic

pip install -U "langchain[anthropic]"
import os
from langchain.chat_models import init_chat_model

os.environ["ANTHROPIC_API_KEY"] = "sk-..."

model = init_chat_model(
    "anthropic:claude-3-5-sonnet-latest",
    temperature=0,
    # other parameters
)

Refer to the API reference for advanced options.

 

プロバイダー固有 LLM の使用

モデルプロバイダーが init_chat_model 経由で利用できない場合、プロバイダーのモデルクラスを直接インスタンス化できます。モデルは BaseChatModel インターフェイスを実装し、ツール呼び出しをサポートしている必要があります :

API リファレンス: ChatAnthropic | create_react_agent

from langchain_anthropic import ChatAnthropic
from langgraph.prebuilt import create_react_agent

model = ChatAnthropic(
    model="claude-3-7-sonnet-latest",
    temperature=0,
    max_tokens=2048
)

agent = create_react_agent(
    model=model,
    # other parameters
)

Illustrative example : The example above uses ChatAnthropic, which is already supported by init_chat_model. This pattern is shown to illustrate how to manually instantiate a model not available through init_chat_model.

 

ストリーミングを無効にする

個々の LLM トークンのストリーミングを無効にするには、モデル初期化時に disable_streaming=True を設定します :

init_chat_model

from langchain.chat_models import init_chat_model

model = init_chat_model(
    "anthropic:claude-3-7-sonnet-latest",
    disable_streaming=True
)

ChatModel

from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-3-7-sonnet-latest",
    disable_streaming=True
)

Refer to the API reference for more information on disable_streaming

 

モデル・フォールバックの追加

model.with_fallbacks([…]) を使用して、別のモデルや別の LLM プロバイダーへのフォールバックを追加できます :

init_chat_model

from langchain.chat_models import init_chat_model

model_with_fallbacks = (
    init_chat_model("anthropic:claude-3-5-haiku-latest")
    .with_fallbacks([
        init_chat_model("openai:gpt-4.1-mini"),
    ])
)

ChatModel

from langchain_anthropic import ChatAnthropic
from langchain_openai import ChatOpenAI

model_with_fallbacks = (
    ChatAnthropic(model="claude-3-5-haiku-latest")
    .with_fallbacks([
        ChatOpenAI(model="gpt-4.1-mini"),
    ])
)

See this guide for more information on model fallbacks.

 

追加リソース

 

以上