Skip to main content

spantree

Trace your AI agent's LLM calls with one line of code.

PyPI License: MIT Python 3.9+

Install

pip install spantree-sdk

Quick Start

from spantree import observe
from anthropic import Anthropic

client = observe(Anthropic())

# All calls are now automatically traced
response = client.messages.create(
    model="claude-sonnet-4-20250514",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=1024,
)

That's it. Every LLM call is captured — model, tokens, latency, input/output — and sent to the Spantree dashboard in the background with zero latency impact.

Grouping Calls with trace()

By default, each LLM call creates its own trace. Use trace() to group multiple calls under a single trace — ideal for agent loops, multi-step workflows, and tool-use patterns.

from spantree import observe, trace
from anthropic import Anthropic

client = observe(Anthropic())

with trace("chat-session"):
    messages = []
    while True:
        messages.append({"role": "user", "content": input("> ")})
        response = client.messages.create(
            model="claude-sonnet-4-20250514",
            messages=messages,
            max_tokens=1024,
        )
        messages.append({"role": "assistant", "content": response.content[0].text})

All LLM calls inside the with trace(...) block share the same trace_id and appear as spans within a single trace in the dashboard.

Tool use

with trace("agent-task"):
    response = client.messages.create(...)   # span 1
    tool_result = run_tool(response)          # not traced (your code)
    response = client.messages.create(...)   # span 2, same trace

Multi-agent

with trace("research-pipeline"):
    plan = orchestrator.messages.create(...)      # span 1
    research = researcher.messages.create(...)    # span 2
    final = orchestrator.messages.create(...)     # span 3

Nested traces

with trace("pipeline") as t:
    print(t.trace_id)  # access the trace ID
    response = client.messages.create(...)       # child of "pipeline"

    with trace("sub-agent"):
        response = client.messages.create(...)   # child of "sub-agent"

    response = client.messages.create(...)       # child of "pipeline"

Without trace() — unchanged behavior

client = observe(Anthropic())
response = client.messages.create(...)  # trace A
response = client.messages.create(...)  # trace B (independent)

Custom Metadata

Attach arbitrary key-value data to spans for filtering and analysis in the dashboard.

Global metadata (all spans from a client)

client = observe(Anthropic(), metadata={"user_id": "u_123", "env": "prod"})

response = client.messages.create(...)  # span gets {"user_id": "u_123", "env": "prod"}

Per-call metadata

client = observe(Anthropic(), metadata={"user_id": "u_123"})

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=1024,
    spantree_metadata={"session_id": "sess_abc", "feature_flag": "new-flow"},
)
# span gets {"user_id": "u_123", "session_id": "sess_abc", "feature_flag": "new-flow"}

Per-call metadata merges with global metadata. Per-call values win on key conflicts.

On trace spans

with trace("my-workflow") as t:
    t.metadata = {"pipeline": "v2", "run_id": "run_xyz"}
    response = client.messages.create(...)

Configuration

Set your API key via environment variable:

export SPANTREE_API_KEY=sk_your_key_here

Or configure programmatically:

from spantree import configure

configure(
    api_key="sk_your_key_here",
    base_url="https://api.spantree.dev",
)

Supported Providers

Provider Wrapper What's traced
Anthropic observe(Anthropic()) messages.create()
OpenAI observe(OpenAI()) chat.completions.create()

How It Works

  1. observe() detects your LLM client type
  2. Wraps API methods to capture timing, tokens, and I/O
  3. Buffers spans in a background thread (zero latency impact)
  4. Flushes batches to the Spantree API every 5s or 100 spans

No monkey-patching. No import hooks. Just a thin wrapper around your existing client.

Requirements

  • Python 3.9+
  • Only runtime dependency: httpx

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

# Clone and install dev dependencies
git clone https://github.com/spantree-io/spantree-python.git
cd spantree-python
pip install -e ".[dev]"

# Run tests
pytest

# Lint
ruff check .

License

MIT

Metadata

Release files for spantree-sdk 0.2.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 spantree-sdk 0.2.0
File Size Uploaded Provenance Actions
spantree_sdk-0.2.0.tar.gz 13.4 kB With provenance Details for spantree_sdk-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spantree-sdk 0.2.0
File Interpreter ABI Platform Actions
spantree_sdk-0.2.0-py3-none-any.whl Python 3 none any Details for spantree_sdk-0.2.0-py3-none-any.whl

Total release size: 23.7 kB

Release files / spantree_sdk-0.2.0.tar.gz

Download URL spantree_sdk-0.2.0.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
83adb300acb08a8dd093fcb05707ea72a71f12db7b12cad4b41416f037d06308
BLAKE2b-256 checksum
How to use checksums
1a29ca1501bd3f4647ea25934434a13af3dab42e003d39eeaaa88baee8da5370
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 May 22, 2026.

Transparency log

Release files / spantree_sdk-0.2.0-py3-none-any.whl

Download URL spantree_sdk-0.2.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ed4f16fc6ddb1839f98dd0b7e265e3180cbc1f5d4b905600bac1d0faa966cc98
BLAKE2b-256 checksum
How to use checksums
293a65b1f6746f1cefbe860471fcfd65eb9189e01a9fa91f92b7bb10bac4bbe4
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 May 22, 2026.

Transparency log

Security for spantree-sdk 0.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for spantree_sdk-0.2.0.tar.gz

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 May 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

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