Skip to main content

Official Python client for the Agent Ledger API

Project description

agent-ledger-sdk

Python SDK for Agent Ledger. Instrument your agents, stream session data, and enforce budget guardrails with a simple HTTP client.

Installation

pip install agent-ledger-sdk
# or
uv add agent-ledger-sdk

Quick start

import os
import time

from agent_ledger_sdk import AgentLedgerClient, BudgetGuardrailError
from openai import OpenAI

ledger = AgentLedgerClient(api_key=os.environ["LEDGER_API_KEY"])
openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

session_id = ledger.start_session("support-bot")
try:
    started = time.perf_counter()
    completion = openai_client.responses.create(
        model="gpt-4o-mini",
        input=[
            {
                "role": "user",
                "content": "Draft a concise welcome email for a premium banking customer.",
            }
        ],
        temperature=0.3,
    )
    latency_ms = int((time.perf_counter() - started) * 1000)
    answer = completion.output[0].content[0].text
    usage = completion.usage

    ledger.log_llm_call(
        session_id,
        {
            "step_index": 0,
            "provider": "openai",
            "model": completion.model,
            "prompt": "Draft a concise welcome email for a premium banking customer.",
            "response": answer,
            "tokens_in": usage.input_tokens,
            "tokens_out": usage.output_tokens,
            "latency_ms": latency_ms,
        },
    )
    ledger.end_session(session_id, "success")
except BudgetGuardrailError as exc:
    print("Budget exceeded", exc.details)
    ledger.end_session(session_id, "error", error_message="Budget hit")
finally:
    ledger.close()

ℹ️ Bring your own OpenAI (or other) API keys. The SDK never proxies LLM calls—you run them directly with your provider’s Python client and log the relevant metadata into Agent Ledger.

API overview

Method Description
AgentLedgerClient(api_key, base_url=None, timeout=10.0) Creates a reusable HTTP client. base_url falls back to AGENT_LEDGER_BASE_URL, otherwise the hosted production endpoint.
start_session(agent_name) Begins a session and returns its ID.
end_session(session_id, status, error_message=None) Completes a session.
log_events(session_id, events) Low-level helper that posts arbitrary event payloads.
log_llm_call, log_tool_call, log_tool_result Typed shortcuts that add the type field for you.

Budget guardrails

When Agent Ledger blocks spending for an agent, the SDK raises BudgetGuardrailError. Inspect the .details attribute to see the remaining budget and attempted spend so you can pause execution gracefully.

Thread safety & async

AgentLedgerClient wraps a single httpx.Client. Reuse the instance across calls and call close() when shutting down, or use it as a context manager. For async workloads, you can build a thin wrapper around httpx.AsyncClient; contributions welcome!

Development

cd packages/sdk-py
python -m venv .venv && source .venv/bin/activate
pip install -e .[dev]
pytest

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

agent_ledger_sdk-0.0.3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

agent_ledger_sdk-0.0.3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file agent_ledger_sdk-0.0.3.tar.gz.

File metadata

  • Download URL: agent_ledger_sdk-0.0.3.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for agent_ledger_sdk-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7314df9bebc5befbf6592b047bb95f12391805b051ec9c231cf9758da30fb8a2
MD5 cbb48703e168124a9fcd7699a64c0213
BLAKE2b-256 d2d9affe80c6808abe9481c106a17f3424e1c095946058f2aa93d1067bdf2e9d

See more details on using hashes here.

File details

Details for the file agent_ledger_sdk-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_ledger_sdk-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 db3382daa3c8516a47eea85cb3793b8e0c7d3ea226e8f1414e4bc811bd3cf69b
MD5 9c6c703f525c21849b6a51f8b663b379
BLAKE2b-256 df5d8d4499d2d27522a17a53455b89c8b6f84d1384b03ddbd89b7068d4e55203

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