## Multiple callback handlers

In the previous examples, we passed in callback handlers upon creation of an object by using `callbacks=`. In this case, the callbacks will be scoped to that particular object.

However, in many cases, it is advantageous to pass in handlers instead when running the object. When we pass through `CallbackHandlers` using the `callbacks` keyword arg when executing an run, those callbacks will be issued by all nested objects involved in the execution. For example, when a handler is passed through to an `Agent`, it will be used for all callbacks related to the agent and all the objects involved in the agent’s execution, in this case, the `Tools`, `LLMChain`, and `LLM`.

This prevents us from having to manually attach the handlers to each individual nested object.

```
from typing import Any, Dict, List, Unionfrom langchain.agents import AgentType, initialize_agent, load_toolsfrom langchain.callbacks.base import BaseCallbackHandlerfrom langchain_core.agents import AgentActionfrom langchain_openai import OpenAI# First, define custom callback handler implementationsclass MyCustomHandlerOne(BaseCallbackHandler):    def on_llm_start(        self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any    ) -> Any:        print(f"on_llm_start {serialized['name']}")    def on_llm_new_token(self, token: str, **kwargs: Any) -> Any:        print(f"on_new_token {token}")    def on_llm_error(        self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any    ) -> Any:        """Run when LLM errors."""    def on_chain_start(        self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any    ) -> Any:        print(f"on_chain_start {serialized['name']}")    def on_tool_start(        self, serialized: Dict[str, Any], input_str: str, **kwargs: Any    ) -> Any:        print(f"on_tool_start {serialized['name']}")    def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:        print(f"on_agent_action {action}")class MyCustomHandlerTwo(BaseCallbackHandler):    def on_llm_start(        self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any    ) -> Any:        print(f"on_llm_start (I'm the second handler!!) {serialized['name']}")# Instantiate the handlershandler1 = MyCustomHandlerOne()handler2 = MyCustomHandlerTwo()# Setup the agent. Only the `llm` will issue callbacks for handler2llm = OpenAI(temperature=0, streaming=True, callbacks=[handler2])tools = load_tools(["llm-math"], llm=llm)agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)# Callbacks for handler1 will be issued by every object involved in the# Agent execution (llm, llmchain, tool, agent executor)agent.run("What is 2 raised to the 0.235 power?", callbacks=[handler1])
```

```
on_chain_start AgentExecutoron_chain_start LLMChainon_llm_start OpenAIon_llm_start (I'm the second handler!!) OpenAIon_new_token  Ion_new_token  needon_new_token  toon_new_token  useon_new_token  aon_new_token  calculatoron_new_token  toon_new_token  solveon_new_token  thison_new_token .on_new_token Actionon_new_token :on_new_token  Calculatoron_new_token Actionon_new_token  Inputon_new_token :on_new_token  2on_new_token ^on_new_token 0on_new_token .on_new_token 235on_new_token on_agent_action AgentAction(tool='Calculator', tool_input='2^0.235', log=' I need to use a calculator to solve this.\nAction: Calculator\nAction Input: 2^0.235')on_tool_start Calculatoron_chain_start LLMMathChainon_chain_start LLMChainon_llm_start OpenAIon_llm_start (I'm the second handler!!) OpenAIon_new_token on_new_token ```texton_new_token on_new_token 2on_new_token **on_new_token 0on_new_token .on_new_token 235on_new_token on_new_token ```on_new_token ...on_new_token numon_new_token expron_new_token .on_new_token evaluateon_new_token ("on_new_token 2on_new_token **on_new_token 0on_new_token .on_new_token 235on_new_token ")on_new_token ...on_new_token on_new_token on_chain_start LLMChainon_llm_start OpenAIon_llm_start (I'm the second handler!!) OpenAIon_new_token  Ion_new_token  nowon_new_token  knowon_new_token  theon_new_token  finalon_new_token  answeron_new_token .on_new_token Finalon_new_token  Answeron_new_token :on_new_token  1on_new_token .on_new_token 17on_new_token 690on_new_token 67on_new_token 372on_new_token 187on_new_token 674on_new_token 
```