Callbacks monitor and log LangChain operations.
Track tokens, costs, and performance.
Callback Types
✅ StdOutCallbackHandler: Print to console
✅ FileCallbackHandler: Log to file
✅ Custom handlers
Example
from langchain.callbacks import StdOutCallbackHandler
handler = StdOutCallbackHandler()
chain = LLMChain(llm=llm, prompt=prompt, callbacks=[handler])
Custom Callback
class MyCallback(BaseCallbackHandler):
def on_llm_start(self, serialized, prompts):
print(f”LLM started with {len(prompts)} prompts”)
Conclusion
Callbacks provide visibility into operations!