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)
    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"
    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.1.tar.gz (6.7 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.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: notilens_langchain-0.1.1.tar.gz
  • Upload date:
  • Size: 6.7 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.1.tar.gz
Algorithm Hash digest
SHA256 b59cfe7018c9d5a4bc4388299c5a85f2d9c1feefaba9a2da5708db55dde5f66b
MD5 7bae3779ee1ed57d87e3590c9a9e2155
BLAKE2b-256 f321eb529df7fa408c0fe4c987c06e8b2d4d9cff64e04691d349d3396ff996bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for notilens_langchain-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 834305d02ada3587b985e6e5b28866bcee0f670390da97c00457ce112315e49e
MD5 151b32c32d04bda43a0d2d89bd409f5e
BLAKE2b-256 9ae089af8ebf4d78ed53b8a2290a8dbf67a74ba7a0b4abc2966797fa1b379c13

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