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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentlensai-0.12.0.tar.gz (100.4 kB view details)

Uploaded Source

Built Distribution

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

agentlensai-0.12.0-py3-none-any.whl (70.1 kB view details)

Uploaded Python 3

File details

Details for the file agentlensai-0.12.0.tar.gz.

File metadata

  • Download URL: agentlensai-0.12.0.tar.gz
  • Upload date:
  • Size: 100.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for agentlensai-0.12.0.tar.gz
Algorithm Hash digest
SHA256 624afcf9dc9a2adad5af10812a97563d29755ee436297801df602e146a475dbe
MD5 df00963265da4d76b815eaf0d8abde51
BLAKE2b-256 e6990201613f3622ad879f1166456a580772c9aa5d3a894835151218f93b44ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for agentlensai-0.12.0.tar.gz:

Publisher: publish-python-sdk.yml on agentkitai/agentlens

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file agentlensai-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: agentlensai-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 70.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for agentlensai-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fdb71e2b4983a1a08d28e16b7df2bb8140c4843c5a9af43672a25fb5de3140df
MD5 ffc0b24e154a579d26414a9a60bcfd24
BLAKE2b-256 d79cafbdbfed8f888559abe97aa2a68eeb0702f42a1c9ea20d70ef9b63619b6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for agentlensai-0.12.0-py3-none-any.whl:

Publisher: publish-python-sdk.yml on agentkitai/agentlens

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 files

0.11.0

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

Supported by

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