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.2.tar.gz (6.8 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.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: notilens_langchain-0.1.2.tar.gz
  • Upload date:
  • Size: 6.8 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.2.tar.gz
Algorithm Hash digest
SHA256 5406558ac69304f47dce5823df8ec2f90ca1b60cff0656f2e80ddfe584eccf5e
MD5 a56d6edd15c3e1e327a160fa97dc6adb
BLAKE2b-256 cb301181adad03b4c268bca62f4600fe7a837248bf35d5ead85a913a6675c22d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for notilens_langchain-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7ecacaa01bc413ee1d14c36a3ebfa72c704848d513739d2806e92cd537cf7084
MD5 dde5209db4cd4d0515c0722e81ed4179
BLAKE2b-256 c424468dc0b030e1f4d9871f8519520809e187e85ce0874d506a624f39c298f0

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