Skip to main content

prism-sdk

LLM Observability in 3 lines. Costs, latency, errors and quality — automatically.

pip install useprism

Quick Start

With Anthropic

import anthropic
import prism

# 1. Init
prism.init("prism_your_api_key")

# 2. Wrap your client
client = prism.wrap(anthropic.Anthropic())

# 3. Use exactly as before — everything is traced automatically
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Summarise this contract..."}],
)

With OpenAI

import openai
import prism

prism.init("prism_your_api_key")
client = prism.wrap(openai.OpenAI())

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)

Streaming

# Anthropic — as an iterator
stream = client.messages.create(model="claude-sonnet-4-6", stream=True,
                                max_tokens=1024, messages=[...])
for event in stream:
    ...

# Anthropic — as a context manager
with client.messages.stream(model="claude-sonnet-4-6", max_tokens=1024, messages=[...]) as s:
    for text in s.text_stream:
        print(text, end="")

The trace is recorded when the stream finishes — output tokens are only known at that point. Events pass through untouched.

Streaming with OpenAI: their API only returns usage if you ask for it. Without it, token counts and cost come back as zero, and the trace is flagged with metadata.usage_missing so you can tell "it was free" apart from "it could not be measured":

stream = client.chat.completions.create(
    model="gpt-4o", stream=True, messages=[...],
    stream_options={"include_usage": True},   # without this, no cost data
)

Async clients

import anthropic, prism

prism.init("prism_your_api_key")
client = prism.wrap(anthropic.AsyncAnthropic())

response = await client.messages.create(model="claude-sonnet-4-6",
                                        max_tokens=1024, messages=[...])

# async streaming
stream = await client.messages.create(stream=True, ...)
async for event in stream:
    ...

AsyncOpenAI works the same way.

When a call fails

Your exception is re-raised untouched — Prism never swallows errors. The trace is recorded first, with the provider's status code and the exception type and message under metadata.error, so a failed call shows why rather than just 500.

try:
    client.messages.create(...)
except anthropic.RateLimitError:
    ...   # your handling is unaffected; the trace already recorded 429

With a custom / unsupported client

import prism
from prism.manual import trace

prism.init("prism_your_api_key")

with trace("my-model", endpoint="/api/summarize") as t:
    result = my_custom_llm_call(prompt)
    t.set_tokens(prompt=500, completion=200)
    t.set_response(result.text)

Configuration

prism.init(
    api_key="prism_your_api_key",
    debug=True,           # print logs to stdout
    flush_interval=2.0,   # seconds between batch flushes
    batch_size=20,        # traces per batch request
)

What gets tracked automatically

Field Description
model Model name (e.g. claude-sonnet-4-6)
provider anthropic / openai / google
prompt_tokens Input tokens used
completion_tokens Output tokens used
cost_usd Calculated cost (server-side)
latency_ms End-to-end response time
status_code 200 on success, 4xx/5xx on errors
prompt_preview First 500 chars of the prompt
response_preview First 500 chars of the response
metadata.error On failure: exception type and message
session_id Conversation grouping (if provided)
prompt_name / prompt_version Via extra_headers

FastAPI integration example

from fastapi import FastAPI, Request
import anthropic
import prism

prism.init("prism_your_api_key")
app = FastAPI()

@app.post("/api/chat")
async def chat(request: Request):
    body = await request.json()

    # Wrap per-request for endpoint-level tracking
    client = prism.wrap(
        anthropic.Anthropic(),
        endpoint="/api/chat",
    )

    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": body["message"]}],
    )
    return {"reply": response.content[0].text}

Overhead

  • ~0ms added to your LLM calls (async background thread)
  • Traces are batched and sent every 2 seconds
  • On shutdown, all pending traces are flushed

License

MIT

Download files

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

Source Distribution

useprism-0.5.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

useprism-0.5.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file useprism-0.5.0.tar.gz.

File metadata

  • Download URL: useprism-0.5.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for useprism-0.5.0.tar.gz
Algorithm Hash digest
SHA256 fe21027d0681c90cda442902ca41bbce4ca11039b9d3d48f11fb21461a608811
MD5 51ee6fbd85772f439c6fb9deedb7d75a
BLAKE2b-256 eec753626897e08cd3a2c2f494a7fba46aa23ccccdce2bbd43b899c08993c02f

See more details on using hashes here.

File details

Details for the file useprism-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: useprism-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for useprism-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3fcfcf5c2bb3a46c0b4e55c9a8fd8769019729ec989f7325afe2d70449078666
MD5 a35c4ae5d913913520e24bac42f28e95
BLAKE2b-256 26a48e813df2c905ab0fbe54c8e6f63e8e8f80bab3a949c07ac24ee0a049f806

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.0

2 files

0.6.0

2 files

This release

0.5.0 This release

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.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