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")
Metadata
Release files for notilens-langchain 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| notilens_langchain-0.2.0.tar.gz | 6.9 kB | Without provenance | Details for notilens_langchain-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| notilens_langchain-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for notilens_langchain-0.2.0-py3-none-any.whl |
Total release size: 14.5 kB
Release files / notilens_langchain-0.2.0.tar.gz
| Download URL | notilens_langchain-0.2.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
18d0203701f831186d8c1cc9bfdffbfe7d48e46c0e5bd3541b33df1061022667
|
|
BLAKE2b-256 checksum How to use checksums |
f6e3f7032c04efb8857e769149a8ad09f95c4465d6f46d6b7ca0519b07b24cb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / notilens_langchain-0.2.0-py3-none-any.whl
| Download URL | notilens_langchain-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5bee5277e3192f83dccb49c5afc7c16ac050ad6940cce8aacca3a2fcf017bdfd
|
|
BLAKE2b-256 checksum How to use checksums |
3f313c0072d4d307693167476d73a321167b6e016ce75bf73ced649b03ce19c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for notilens-langchain 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.