Quick Start

This guide will have you tracking AI costs in under 5 minutes.

Step 1: Install the SDK

pip install infraprism

Step 2: Initialize the Client

Replace your existing OpenAI client with InfraPrism’s drop-in replacement:

# Before
from openai import OpenAI
client = OpenAI()

# After
from infraprism import InfraPrismOpenAI
client = InfraPrismOpenAI()

That’s it! All your OpenAI calls are now tracked.

Step 3: Add Cost Attribution

To attribute costs to specific customers, teams, or projects, add entity parameters:

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the capital of France?"}
    ],
    # Attribution parameters
    entity_type="customer",      # customer | team | project | employee
    entity_id="acme-corp",       # Your internal identifier
    tags={"feature": "chatbot", "env": "production"},  # Optional metadata
)

Step 4: View Your Dashboard

Log in to app.infraprism.com to see:

  • Real-time costs as calls come in
  • Cost breakdown by entity (customer, team, project)
  • Usage patterns over time
  • Anomaly alerts when spending spikes

Complete Example

Here’s a complete example showing common patterns:

from infraprism import InfraPrismOpenAI
import os

# Initialize the client (keys from environment variables)
client = InfraPrismOpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    infraprism_api_key=os.getenv("INFRAPRISM_API_KEY"),
)

def chat_with_customer(customer_id: str, user_message: str) -> str:
    """Handle a chat request and track costs to the customer."""
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_message}
        ],
        entity_type="customer",
        entity_id=customer_id,
        tags={
            "feature": "customer-chat",
            "env": os.getenv("ENVIRONMENT", "development"),
        },
    )
    return response.choices[0].message.content

# Usage
response = chat_with_customer("acme-corp", "How do I reset my password?")
print(response)

Streaming Responses

Streaming works exactly the same way:

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a haiku about Python"}],
    stream=True,
    entity_type="team",
    entity_id="engineering",
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Streaming responses are tracked as a single event with the total token count.

What Gets Tracked?

InfraPrism only captures metadata—never your actual prompts or responses:

TrackedNot Tracked
Token countsPrompt content
Model usedResponse content
LatencySystem messages
CostImages or files
Your entity tagsAny actual data

Next Steps