NotiLens callback handler for LangChain — automatic run tracking, token metrics, and error alerting
Project description
notilens-langchain
NotiLens callback handler for LangChain. Automatically tracks every chain, agent, LLM call, tool invocation, and retriever query — sending lifecycle events, token metrics, durations, and errors to NotiLens with zero manual instrumentation.
Installation
pip install notilens-langchain
Quick start
from notilens_langchain import NotiLensCallbackHandler
handler = NotiLensCallbackHandler(
name="my-app",
token="YOUR_TOKEN", # or set NOTILENS_TOKEN env var
secret="YOUR_SECRET", # or set NOTILENS_SECRET env var
)
Attach to any chain or LLM — that's it:
# On a chain
result = chain.invoke({"input": "..."}, config={"callbacks": [handler]})
# On an LLM
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])
# On an agent
agent_executor.invoke({"input": "..."}, config={"callbacks": [handler]})
What gets tracked automatically
| Event | NotiLens action |
|---|---|
| Chain starts | run.start() |
| Chain completes | run.complete() |
| Chain errors | run.fail(error) |
| LLM / chat model called | run.progress("Calling gpt-4o…") |
| LLM response received | run.metric(prompt_tokens, completion_tokens, total_tokens) + duration |
LLM timeout (> call_timeout) |
run.timeout() |
| LLM error | run.error() |
| Agent uses a tool | run.loop("Agent step #N: using tool 'X'") + run.progress("Step N: using tool 'X'") |
| Agent finishes | run.complete(output) |
| Tool starts | run.progress("Running tool 'calculator'…") |
| Tool completes | run.progress("Tool completed") |
| Tool errors | run.error() |
| Retriever starts | run.progress("Retrieving context for: …") |
| Retriever returns docs | run.progress("Retrieved 5 documents") |
| Retriever errors | run.error() |
Configuration
handler = NotiLensCallbackHandler(
name="my-app", # name shown in NotiLens
token="YOUR_TOKEN", # NotiLens topic token
secret="YOUR_SECRET", # NotiLens topic secret
task="rag-pipeline", # optional fixed task label (default: chain class name)
call_timeout=30.0, # LLM seconds before timeout event fires (default: 30)
send_output=True, # send final agent output as output.generated event (default: False)
max_output_length=2000, # max characters of output to send (default: 2000)
min_level="info", # minimum event level: "info", "warning", "error"
)
Custom metrics & events
You can set custom metrics or fire custom events on the active run directly from the handler:
# Numeric values accumulate across calls; strings are replaced
handler.metric('cost_usd', 0.004)
handler.metric('cache_hits', 1)
handler.metric('model', 'gpt-4o')
# Fire a custom event with an optional level and metadata
handler.track('cache.hit', 'Retrieved from vector cache', level='info')
handler.track('guardrail.triggered', 'Blocked unsafe output', level='warning')
# Send a rich notification with URLs and tags
handler.notify('report.ready', 'Summary complete',
download_url='https://example.com/report.pdf',
tags='langchain,rag',
)
handler.notify('result.image', 'Chart generated',
image_url='https://example.com/chart.png',
open_url='https://example.com/dashboard',
)
Note: Calling
metric(),track(), ornotify()when no chain is running logs a warning and does nothing:WARNING NotiLens: handler.metric('cost_usd', ...) called with no active run — ignoring.
Reusing an existing NotiLens instance
import notilens
from notilens_langchain import NotiLensCallbackHandler
nl = notilens.init("my-app", token="...", secret="...")
handler = NotiLensCallbackHandler(nl_agent=nl)
Environment variables
export NOTILENS_TOKEN="your_token"
export NOTILENS_SECRET="your_secret"
# Token and secret are picked up automatically
handler = NotiLensCallbackHandler(name="my-app")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file notilens_langchain-0.2.0.tar.gz.
File metadata
- Download URL: notilens_langchain-0.2.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18d0203701f831186d8c1cc9bfdffbfe7d48e46c0e5bd3541b33df1061022667
|
|
| MD5 |
24e70ccde8db91f797482dc762c04884
|
|
| BLAKE2b-256 |
f6e3f7032c04efb8857e769149a8ad09f95c4465d6f46d6b7ca0519b07b24cb4
|
File details
Details for the file notilens_langchain-0.2.0-py3-none-any.whl.
File metadata
- Download URL: notilens_langchain-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bee5277e3192f83dccb49c5afc7c16ac050ad6940cce8aacca3a2fcf017bdfd
|
|
| MD5 |
38dd8ec238fea409ba10e330a209a1da
|
|
| BLAKE2b-256 |
3f313c0072d4d307693167476d73a321167b6e016ce75bf73ced649b03ce19c0
|