Skip to main content

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

Project description

AgentLens Python SDK

PyPI Python License: MIT

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

Installation

pip install agentlensai

Quick Start

Sync Client

from agentlensai import AgentLensClient, LogLlmCallParams, LlmMessage, TokenUsage

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

# Query events
result = client.query_events()
print(f"Total events: {result.total}")

# Get sessions
sessions = client.get_sessions()
for session in sessions.sessions:
    print(f"Session {session.id}: {session.status}")

# Log an LLM call
result = client.log_llm_call(
    session_id="ses_abc",
    agent_id="my-agent",
    params=LogLlmCallParams(
        provider="anthropic",
        model="claude-sonnet-4-20250514",
        messages=[LlmMessage(role="user", content="Hello!")],
        completion="Hi there! How can I help?",
        finish_reason="stop",
        usage=TokenUsage(input_tokens=10, output_tokens=8, total_tokens=18),
        cost_usd=0.001,
        latency_ms=850,
    ),
)
print(f"Logged LLM call: {result.call_id}")

# Get LLM analytics
analytics = client.get_llm_analytics()
print(f"Total LLM calls: {analytics.summary.total_calls}")
print(f"Total cost: ${analytics.summary.total_cost_usd:.2f}")

client.close()

Async Client

import asyncio
from agentlensai import AsyncAgentLensClient

async def main():
    async with AsyncAgentLensClient("http://localhost:3400", api_key="als_your_key") as client:
        # All the same methods, but async
        result = await client.query_events()
        health = await client.health()
        print(f"Server: {health.version}, Events: {result.total}")

asyncio.run(main())

Privacy-Aware Logging

# Redact sensitive prompts/completions while keeping metadata
result = client.log_llm_call(
    session_id="ses_abc",
    agent_id="my-agent",
    params=LogLlmCallParams(
        provider="openai",
        model="gpt-4o",
        messages=[LlmMessage(role="user", content="My SSN is 123-45-6789")],
        completion="I'll process that...",
        finish_reason="stop",
        usage=TokenUsage(input_tokens=15, output_tokens=10, total_tokens=25),
        cost_usd=0.002,
        latency_ms=1200,
        redact=True,  # Content replaced with [REDACTED], metadata preserved
    ),
)

Auto-Instrumentation (v0.4.0+)

One line of setup — every LLM call captured automatically. No code changes needed.

pip install agentlensai[openai]      # or agentlensai[anthropic] or agentlensai[all]
import agentlensai

# Automatically instruments OpenAI + Anthropic SDKs
agentlensai.init(
    url="http://localhost:3400",
    api_key="als_your_key",
    agent_id="my-agent",
)

# Every call is now captured — deterministic, not MCP-dependent
import openai
client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
)
# ^ Automatically logged: model, tokens, cost, latency, prompts

# Works with Anthropic too
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
# ^ Also captured automatically

# Clean up when done
agentlensai.shutdown()

LangChain Integration

from agentlensai.integrations.langchain import AgentLensCallbackHandler

handler = AgentLensCallbackHandler()
chain.invoke(input, config={"callbacks": [handler]})
# ^ Every LLM call, tool call, and chain event captured

Key Guarantees

  • Deterministic — Every call captured, not dependent on LLM behavior
  • Fail-safe — If AgentLens server is down, your code still works normally
  • Zero overhead — Events sent via background thread, doesn't block your calls
  • Privacyinit(redact=True) strips content, keeps metadata

Features

  • Auto-Instrumentation — One-liner setup for OpenAI, Anthropic, LangChain
  • Sync & Async — Both AgentLensClient and AsyncAgentLensClient
  • Typed — Full Pydantic v2 models, PEP 561 py.typed marker
  • LLM Call Tracking — Log prompts, completions, tokens, costs, latency
  • Privacy Redaction — Strip sensitive content while keeping analytics metadata
  • Error HierarchyAgentLensError, AuthenticationError, NotFoundError, ValidationError, AgentLensConnectionError
  • Context Managerswith / async with for automatic cleanup

API Reference

Method Description
query_events(query?) Query events with filters and pagination
get_event(id) Get a single event by ID
get_sessions(query?) Query sessions
get_session(id) Get a single session
get_session_timeline(session_id) Get session timeline with hash chain verification
log_llm_call(session_id, agent_id, params) Log an LLM call with paired events
get_llm_analytics(params?) Get LLM cost/usage analytics
health() Check server health

Documentation

Full docs: amitpaz1.github.io/agentlens

Development

pip install -e ".[dev]"
pytest                    # 107 tests
mypy src/ --strict        # Type checking
ruff check src/ tests/    # Linting

License

MIT

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

agentlensai-0.4.0.tar.gz (30.9 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.4.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentlensai-0.4.0.tar.gz
  • Upload date:
  • Size: 30.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for agentlensai-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0b37f6d5848fd8da1570a1fd57bbd927262950f839e92215bcc5a4cfe5a12d08
MD5 8773e2193d5b73acd38352063b4b7dc3
BLAKE2b-256 1e5a06d138421c1f63453c6d7cd5fd717712a4db6dec6e2448e96728527a227f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: agentlensai-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for agentlensai-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cba6a54d07cd9cede0a1ed8b0e81434699424e9d41a4f6be19f88bd427412ebe
MD5 495f0d3c3e697ba3db2005172d073dcc
BLAKE2b-256 5bb52215e53fb2a4a0d861603894cb3c08f285b6184c3cee41f752425194ae32

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