Skip to main content

agentlensai

Python SDK for AgentLens — observability and audit trail for AI agents.

Installation

pip install agentlensai

Quick Start — Auto-Instrumentation

The fastest way to get started. One call instruments all installed LLM providers automatically.

import agentlensai

session_id = agentlensai.init(
    agent_id="my-agent",
    api_key="als_xxx",  # or set AGENTLENS_API_KEY
)

# That's it! All OpenAI/Anthropic/etc. calls are now tracked.
import openai
client = openai.OpenAI()
client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello"}],
)
# ^ This call is automatically captured by AgentLens

# When done:
agentlensai.shutdown()

init() Parameters

Parameter Default Description
url http://localhost:3400 Server URL (positional)
server_url — Server URL (keyword, takes precedence)
cloud False Use AgentLens Cloud (https://api.agentlens.ai)
api_key AGENTLENS_API_KEY env API key
agent_id "default" Agent identifier
session_id auto-generated Session ID
redact False Strip prompt/completion content
pii_patterns None List of regex patterns for PII filtering
pii_filter None Custom filter function for strings
sync_mode False Send events synchronously
integrations "auto" Which providers to instrument

Environment Variables

Env Var Description
AGENTLENS_SERVER_URL Server URL
AGENTLENS_API_KEY API key

Manual Client Usage

from agentlensai import AgentLensClient

client = AgentLensClient("http://localhost:3400", api_key="als_xxx")

# Query events
result = client.query_events(agent_id="my-agent", limit=10)
for event in result.events:
    print(event.event_type, event.timestamp)

# Log an LLM call manually
from agentlensai import LogLlmCallParams, LlmMessage, TokenUsage

result = client.log_llm_call(
    session_id="sess-1",
    agent_id="my-agent",
    params=LogLlmCallParams(
        provider="openai",
        model="gpt-4",
        messages=[LlmMessage(role="user", content="Hello")],
        completion="Hi there!",
        finish_reason="stop",
        usage=TokenUsage(input_tokens=5, output_tokens=3, total_tokens=8),
        cost_usd=0.001,
        latency_ms=450,
    ),
)
print(result.call_id)

# Context manager
with AgentLensClient("http://localhost:3400") as client:
    health = client.health()

Async Client

from agentlensai import AsyncAgentLensClient

async def main():
    async with AsyncAgentLensClient("http://localhost:3400", api_key="als_xxx") as client:
        events = await client.query_events(agent_id="my-agent")
        health = await client.health()

PII Filtering

Filter sensitive data before it leaves your application.

Built-in Patterns

import agentlensai
from agentlensai import PII_EMAIL, PII_SSN, PII_CREDIT_CARD, PII_PHONE

agentlensai.init(
    agent_id="my-agent",
    pii_patterns=[PII_EMAIL, PII_SSN, PII_CREDIT_CARD, PII_PHONE],
)
# All email addresses, SSNs, credit cards, and phone numbers
# are replaced with [REDACTED] before sending.

Custom Filter

import re

def my_filter(text: str) -> str:
    return re.sub(r"password=\S+", "password=[REDACTED]", text)

agentlensai.init(
    agent_id="my-agent",
    pii_filter=my_filter,
)

Full Redaction

agentlensai.init(agent_id="my-agent", redact=True)
# All prompt/completion content is stripped entirely.

Provider Examples

OpenAI

import agentlensai
import openai

agentlensai.init(agent_id="my-agent")

client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Explain quantum computing"}],
)
# Automatically tracked!

Anthropic

import agentlensai
import anthropic

agentlensai.init(agent_id="my-agent")

client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello, Claude"}],
)
# Automatically tracked!

Selective Instrumentation

# Only instrument OpenAI
agentlensai.init(agent_id="my-agent", integrations="openai")

# Instrument specific providers
agentlensai.init(agent_id="my-agent", integrations=["openai", "anthropic"])

Error Handling

from agentlensai import (
    AgentLensError,
    AuthenticationError,
    NotFoundError,
    ValidationError,
    AgentLensConnectionError,
    RateLimitError,
    QuotaExceededError,
    BackpressureError,
)

try:
    client.get_event("bad-id")
except NotFoundError:
    print("Event not found")
except RateLimitError as e:
    print(f"Rate limited, retry after {e.retry_after}s")
except AgentLensConnectionError:
    print("Server unreachable")

Lessons API (Deprecated)

Lesson methods are deprecated. Use lore-sdk instead.

Metadata

Release files for agentlensai 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentlensai 0.12.0
File Size Uploaded
agentlensai-0.12.0.tar.gz 100.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agentlensai 0.12.0
File Interpreter ABI Platform
agentlensai-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 170.4 kB

Release files / agentlensai-0.12.0.tar.gz

Download URL agentlensai-0.12.0.tar.gz
Size 100.4 kB
Tags Source
SHA-256 checksum
How to use checksums
624afcf9dc9a2adad5af10812a97563d29755ee436297801df602e146a475dbe
BLAKE2b-256 checksum
How to use checksums
e6990201613f3622ad879f1166456a580772c9aa5d3a894835151218f93b44ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.

Transparency log

Release files / agentlensai-0.12.0-py3-none-any.whl

Download URL agentlensai-0.12.0-py3-none-any.whl
Size 70.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdb71e2b4983a1a08d28e16b7df2bb8140c4843c5a9af43672a25fb5de3140df
BLAKE2b-256 checksum
How to use checksums
d79cafbdbfed8f888559abe97aa2a68eeb0702f42a1c9ea20d70ef9b63619b6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page