Skip to main content

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(
    agent="my-agent",
    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.progress("Step N: using tool 'web_search'")
Agent loops (> loop_threshold steps) run.loop()
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(
    agent="my-agent",           # agent name 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)
    loop_threshold=10,          # agent steps before loop alert fires (default: 10)
    call_timeout=30.0,          # LLM seconds before timeout event fires (default: 30)
    min_level="info",           # minimum event level: "info", "warning", "error"
    silent=False,               # suppress SDK log output
)

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')

Note: Calling metric() or track() 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 agent

import notilens
from notilens_langchain import NotiLensCallbackHandler

nl = notilens.init("my-agent", 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(agent="my-agent")

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

notilens_langchain-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notilens_langchain-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file notilens_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: notilens_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for notilens_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa2a3e30b2dd15b720f2a1954217c678f30715113004c435ba08d9363617342f
MD5 53dcf6c0069d79874802c04a24c6f346
BLAKE2b-256 8f498e79fb94c9392430d974a226dbd3122e3e00992cf88f4c373f925d524c30

See more details on using hashes here.

File details

Details for the file notilens_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for notilens_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3edab134c2ce85cf4297569d9501fd923c3edb5e932bc3bc983cb0b7ff6181a1
MD5 b017f418d1ebbe2272a83e598d32afa5
BLAKE2b-256 95beb149edcae4106690af2c6eb5912ead2b05dd020d9ea3b4ccabf675178333

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page