Skip to main content

Stoa Python SDK

Business telemetry and payments for AI products.

Stoa instruments native AI provider clients, attaches your application's user_id, and helps you understand which users, features, and product areas are driving AI usage, cost, and outcomes.

Installation

pip install onstoa

Quick Start

from openai import OpenAI
from stoa import Stoa

stoa = Stoa()

# Idempotently create or update the user profile in Stoa.
stoa.identify(
    user_id=user.id,
    metadata={"plan": "pro"},
)

# Build the native provider client in your app, then wrap it with Stoa.
openai = stoa.openai(
    OpenAI(timeout=30),
    user_id=user.id,
)


@stoa.context(feature="summary_export", product="docs")
def generate_summary(document: str):
    response = openai.responses.create(
        model="gpt-4.1-mini",
        input=f"Summarize this document:\n\n{document}",
    )

    stoa.track(
        "summary_export.completed",
        user_id=user.id,
        metadata={"format": "pdf"},
    )

    return response

Identify Users

Call identify when a user signs up, logs in, or when you want to sync stable user metadata.

stoa.identify(
    user_id="user_123",
    metadata={
        "plan": "enterprise",
        "workspace_id": "ws_456",
    },
)

identify is idempotent. It creates the user profile if it does not exist and updates metadata if it does. It does not set active identity; provider instrumentation and custom events still require an explicit user_id.

Instrument AI Providers

Construct provider clients exactly as you would without Stoa, then pass them to Stoa.

from openai import OpenAI

openai = stoa.openai(
    OpenAI(api_key="sk-...", timeout=30, max_retries=2),
    user_id=user.id,
)

Every provider call made through the returned client is attributed to that user_id.

OpenAI

openai = stoa.openai(OpenAI(), user_id=user.id)

response = openai.responses.create(
    model="gpt-4.1-mini",
    input="Write a release note",
)

Anthropic

from anthropic import Anthropic

anthropic = stoa.anthropic(Anthropic(), user_id=user.id)

message = anthropic.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a release note"}],
)

ElevenLabs

from elevenlabs.client import ElevenLabs

elevenlabs = stoa.elevenlabs(ElevenLabs(), user_id=user.id)

audio = elevenlabs.text_to_speech.convert(
    voice_id="JBFqnCBsd6RMkjVDRZzb",
    model_id="eleven_multilingual_v2",
    text="Welcome back",
)

OpenRouter

openrouter = stoa.openrouter(
    OpenAI(
        api_key=os.environ["OPENROUTER_API_KEY"],
        base_url="https://openrouter.ai/api/v1",
    ),
    user_id=user.id,
)

Add Context

Use context to attach feature, product, workflow, experiment, or route metadata to work done inside a scope.

with stoa.context(feature="chat", product="assistant"):
    openai.responses.create(...)

context can also be used as a decorator.

@stoa.context(feature="summary_export", product="docs")
def generate_summary(document: str):
    return openai.responses.create(
        model="gpt-4.1-mini",
        input=document,
    )

Nested context merges automatically. Inner values override outer values.

Track Custom Events

App-defined business events.

stoa.track(
    "report.exported",
    user_id=user.id,
    metadata={
        "format": "csv",
        "rows": 1200,
    },
)

Tracked events inherit active context metadata.

Create Payments

Use create_payment when you want to start a Stripe payment flow for a user.

payment = stoa.create_payment(
    user_id=user.id,
    email=user.email,
    return_url="https://app.example.com/billing/return",
)

redirect_user_to(payment.checkout_url)

Billing Webhooks

Stoa's SDK does not block AI provider calls by default.

Use webhooks to react to billing events such as:

balance.low
balance.depleted
payment.succeeded
payment.failed
subscription.updated

Your app decides what to do when those events arrive, such as notifying the user, prompting for payment, or changing access.

Environment Variables

STOA_API_KEY=...
STOA_BASE_URL=https://www.onstoa.com/api

Metadata

Release files for onstoa 0.2.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for onstoa 0.2.12
File Size Uploaded Provenance Actions
onstoa-0.2.12.tar.gz 145.5 kB With provenance Details for onstoa-0.2.12.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for onstoa 0.2.12
File Interpreter ABI Platform Actions
onstoa-0.2.12-py3-none-any.whl Python 3 none any Details for onstoa-0.2.12-py3-none-any.whl

Total release size: 170.6 kB

Release files / onstoa-0.2.12.tar.gz

Download URL onstoa-0.2.12.tar.gz
Size 145.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c992b6306529e9d1975c8d9faae6d23a62255608d84f52f421b60db65d31a6a5
BLAKE2b-256 checksum
How to use checksums
b6d3d9f9b8d729079a0c985bd57a821ccba5882bcd1172f10336f6817ee59db0
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 14, 2026.

Transparency log

Release files / onstoa-0.2.12-py3-none-any.whl

Download URL onstoa-0.2.12-py3-none-any.whl
Size 25.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
958b915cab6ae97db18dbb6b57bb79f36f2348c1eec1f973612a8ca792d9114c
BLAKE2b-256 checksum
How to use checksums
e4ad04215c277880cf24f94c19762de2802aec81079e46702bc94a179bb45d7f
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 14, 2026.

Transparency log

Security for onstoa 0.2.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.

Full provenance

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

Select from 2 files

Attestation for onstoa-0.2.12.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 14, 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.

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