Skip to main content

Track AI API usage with Joule - works with OpenAI and Anthropic SDKs

Project description

Joule SDK for Python

Track AI API usage with automatic monitoring of costs, tokens, and performance. Works with OpenAI and Anthropic SDKs.

Installation

pip install joule-sdk

# With OpenAI support
pip install joule-sdk[openai]

# With Anthropic support
pip install joule-sdk[anthropic]

# With both
pip install joule-sdk[all]

Quick Start

OpenAI

from openai import OpenAI
from joule_sdk import JouleOpenAI

# Create your OpenAI client as usual
client = OpenAI(api_key="sk-...")

# Wrap it with Joule
joule = JouleOpenAI(
    client,
    api_key="your-joule-api-key",
    user_id="user-123",           # optional
    environment="production"       # optional
)

# Use it exactly like the regular OpenAI client
response = joule.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Usage is automatically tracked to Joule

Anthropic

from anthropic import Anthropic
from joule_sdk import JouleAnthropic

# Create your Anthropic client as usual
client = Anthropic(api_key="sk-ant-...")

# Wrap it with Joule
joule = JouleAnthropic(
    client,
    api_key="your-joule-api-key",
    user_id="user-123",
    environment="production"
)

# Use it exactly like the regular Anthropic client
response = joule.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

# Usage is automatically tracked to Joule

Streaming (Anthropic)

with joule.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

# Usage is tracked when stream completes

What Gets Tracked

The SDK automatically tracks:

  • Tokens: Input, output, and total tokens
  • Cost: Calculated from current pricing
  • Performance: Response time in milliseconds
  • Errors: Error types and messages
  • Context: User ID, team ID, environment

Privacy: Only metadata is tracked. Your prompts and responses are never sent to Joule.

Configuration

joule = JouleOpenAI(
    client,
    api_key="your-joule-api-key",      # Required
    base_url="https://api.joule.ai",   # Optional, defaults to production
    user_id="user-123",                 # Optional, default user ID
    team_id="team-456",                 # Optional, default team ID
    application_id="my-app",            # Optional, application identifier
    environment="production",           # Optional, deployment environment
    debug=False                         # Optional, enable debug logging
)

Per-Request Context

Override defaults for specific requests:

response = joule.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    joule_user_id="specific-user",      # Override user ID
    joule_team_id="specific-team",      # Override team ID
    joule_metadata={"feature": "chat"}  # Add custom metadata
)

Context Manager

Use as a context manager to ensure events are flushed:

with JouleOpenAI(client, api_key="...") as joule:
    response = joule.chat.completions.create(...)
# Events are flushed when exiting the context

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

joule_sdk-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

joule_sdk-0.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file joule_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: joule_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for joule_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea442f5dff617f462a63d36e85e5957623d6355d5af33502be14dfa2de56fd96
MD5 491c4495b347f8353dd91835ed15c3fa
BLAKE2b-256 32b2abda41bdac1087e0fa37e9b0fdec604b40049f405a9eead2452256bd8cbb

See more details on using hashes here.

File details

Details for the file joule_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: joule_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for joule_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3277ffecf07f1c17393af9749605db301ee49d5a3eccf946372b5c59e0d95e24
MD5 88fb1ad52b773579e4f6ee0eb3530019
BLAKE2b-256 5653e517572b2e9e0ee55cdffbf3f3c70d2758944c9ba5e44bc3e5227b37a564

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