Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open on GitHub

Callbacks

Prerequisites

LangChain provides a callback system that allows you to hook into the various stages of your LLM application. This is useful for logging, monitoring, streaming, and other tasks.

You can subscribe to these events by using thecallbacks argument available throughout the API. This argument is a list of handler objects, which are expected to implement one or more of the methods described below in more detail.

Callback events

EventEvent TriggerAssociated Method
Chat model startWhen a chat model startson_chat_model_start
LLM startWhen a llm startson_llm_start
LLM new tokenWhen an llm OR chat model emits a new tokenon_llm_new_token
LLM endsWhen an llm OR chat model endson_llm_end
LLM errorsWhen an llm OR chat model errorson_llm_error
Chain startWhen a chain starts runningon_chain_start
Chain endWhen a chain endson_chain_end
Chain errorWhen a chain errorson_chain_error
Tool startWhen a tool starts runningon_tool_start
Tool endWhen a tool endson_tool_end
Tool errorWhen a tool errorson_tool_error
Agent actionWhen an agent takes an actionon_agent_action
Agent finishWhen an agent endson_agent_finish
Retriever startWhen a retriever startson_retriever_start
Retriever endWhen a retriever endson_retriever_end
Retriever errorWhen a retriever errorson_retriever_error
TextWhen arbitrary text is runon_text
RetryWhen a retry event is runon_retry

Callback handlers

Callback handlers can either besync orasync:

During run-time LangChain configures an appropriate callback manager (e.g.,CallbackManager orAsyncCallbackManager which will be responsible for calling the appropriate method on each "registered" callback handler when the event is triggered.

Passing callbacks

Thecallbacks property is available on most objects throughout the API (Models, Tools, Agents, etc.) in two different places:

  • Request time callbacks: Passed at the time of the request in addition to the input data.Available on all standardRunnable objects. These callbacks are INHERITED by all childrenof the object they are defined on. For example,chain.invoke({"number": 25}, {"callbacks": [handler]}).
  • Constructor callbacks:chain = TheNameOfSomeChain(callbacks=[handler]). These callbacksare passed as arguments to the constructor of the object. The callbacks are scopedonly to the object they are defined on, and arenot inherited by any children of the object.
warning

Constructor callbacks are scoped only to the object they are defined on. They arenot inherited by childrenof the object.

If you're creating a custom chain or runnable, you need to remember to propagate request timecallbacks to any child objects.

Async in Python<=3.10

AnyRunnableLambda, aRunnableGenerator, orTool that invokes other runnablesand is runningasync in python<=3.10, will have to propagate callbacks to childobjects manually. This is because LangChain cannot automatically propagatecallbacks to child objects in this case.

This is a common reason why you may fail to see events being emitted from customrunnables or tools.

For specifics on how to use callbacks, see therelevant how-to guides here.


[8]ページ先頭

©2009-2025 Movatter.jp