*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to create custom callback handlers

On this page

How to create custom callback handlers
======================================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Callbacks](/v0.2/docs/concepts/#callbacks)

LangChain has some built-in callback handlers, but you will often want to create your own handlers with custom logic.

To create a custom callback handler, we need to determine the [event(s)](https://api.python.langchain.com/en/latest/callbacks/langchain_core.callbacks.base.BaseCallbackHandler.html#langchain-core-callbacks-base-basecallbackhandler) we want our callback handler to handle as well as what we want our callback handler to do when the event is triggered. Then all we need to do is attach the callback handler to the object, for example via [the constructor](/v0.2/docs/how_to/callbacks_constructor/) or [at runtime](/v0.2/docs/how_to/callbacks_runtime/).

In the example below, we'll implement streaming with a custom handler.

In our custom callback handler `MyCustomHandler`, we implement the `on_llm_new_token` handler to print the token we have just received. We then attach our custom handler to the model object as a constructor callback.

    from langchain_anthropic import ChatAnthropicfrom langchain_core.callbacks import BaseCallbackHandlerfrom langchain_core.prompts import ChatPromptTemplateclass MyCustomHandler(BaseCallbackHandler):    def on_llm_new_token(self, token: str, **kwargs) -> None:        print(f"My custom handler, token: {token}")prompt = ChatPromptTemplate.from_messages(["Tell me a joke about {animal}"])# To enable streaming, we pass in `streaming=True` to the ChatModel constructor# Additionally, we pass in our custom handler as a list to the callbacks parametermodel = ChatAnthropic(    model="claude-3-sonnet-20240229", streaming=True, callbacks=[MyCustomHandler()])chain = prompt | modelresponse = chain.invoke({"animal": "bears"})

**API Reference:**[ChatAnthropic](https://api.python.langchain.com/en/latest/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html) | [BaseCallbackHandler](https://api.python.langchain.com/en/latest/callbacks/langchain_core.callbacks.base.BaseCallbackHandler.html) | [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html)

    My custom handler, token: HereMy custom handler, token: 'sMy custom handler, token:  aMy custom handler, token:  bearMy custom handler, token:  jokeMy custom handler, token:  forMy custom handler, token:  youMy custom handler, token: :My custom handler, token: WhyMy custom handler, token:  diMy custom handler, token: d theMy custom handler, token:  bearMy custom handler, token:  dissolMy custom handler, token: veMy custom handler, token:  inMy custom handler, token:  waterMy custom handler, token: ?My custom handler, token: BecauseMy custom handler, token:  itMy custom handler, token:  wasMy custom handler, token:  aMy custom handler, token:  polarMy custom handler, token:  bearMy custom handler, token: !

You can see [this reference page](https://api.python.langchain.com/en/latest/callbacks/langchain_core.callbacks.base.BaseCallbackHandler.html#langchain-core-callbacks-base-basecallbackhandler) for a list of events you can handle. Note that the `handle_chain_*` events run for most LCEL runnables.

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You've now learned how to create your own custom callback handlers.

Next, check out the other how-to guides in this section, such as [how to attach callbacks to a runnable](/v0.2/docs/how_to/callbacks_attach/).

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/custom_callbacks.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to do retrieval with contextual compression

](/v0.2/docs/how_to/contextual_compression/)[

Next

How to create a custom chat model class

](/v0.2/docs/how_to/custom_chat_model/)

*   [Next steps](#next-steps)