Skip to main content

Callbacks

Prerequisites

LangChain provides a callbacks 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 the callbacks argument available throughout the API. This argument is 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 be sync or async:

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

Passing callbacks​

The callbacks 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 standard Runnable objects. These callbacks are INHERITED by all children of the object they are defined on. For example, chain.invoke({"number": 25}, {"callbacks": [handler]}).
  • Constructor callbacks: chain = TheNameOfSomeChain(callbacks=[handler]). These callbacks are passed as arguments to the constructor of the object. The callbacks are scoped only to the object they are defined on, and are not inherited by any children of the object.
warning

Constructor callbacks are scoped only to the object they are defined on. They are not inherited by children of the object.

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

Async in Python<=3.10

Any RunnableLambda, a RunnableGenerator, or Tool that invokes other runnables and is running async in python<=3.10, will have to propagate callbacks to child objects manually. This is because LangChain cannot automatically propagate callbacks to child objects in this case.

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

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


Was this page helpful?