OpenAI Provider
InfraPrism provides a drop-in replacement for the official OpenAI Python SDK. All OpenAI features are supported with zero code changes required beyond the import.
Installation
pip install infraprism
The InfraPrism SDK includes the OpenAI SDK as a dependency.
Basic Usage
Replace your OpenAI import with InfraPrism:
# Before
from openai import OpenAI
client = OpenAI()
# After
from infraprism import InfraPrismOpenAI
client = InfraPrismOpenAI()
All existing code continues to work:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello, world!"}],
)
print(response.choices[0].message.content)
Adding Cost Attribution
Add entity parameters to track costs by customer, team, project, or employee:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Explain quantum computing"}],
entity_type="customer",
entity_id="acme-corp",
tags={"feature": "ai-assistant", "tier": "enterprise"},
)
Supported Models
InfraPrism tracks costs for all OpenAI models:
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| gpt-4o | $2.50 | $10.00 |
| gpt-4o-mini | $0.15 | $0.60 |
| gpt-4-turbo | $10.00 | $30.00 |
| gpt-4 | $30.00 | $60.00 |
| gpt-3.5-turbo | $0.50 | $1.50 |
| o1-preview | $15.00 | $60.00 |
| o1-mini | $3.00 | $12.00 |
Prices are updated automatically. Check the dashboard for current rates.
Streaming
Streaming responses are fully supported:
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a story"}],
stream=True,
entity_type="team",
entity_id="content",
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Token counts and costs are calculated after the stream completes and tracked as a single event.
Async Support
Use the async client for async/await patterns:
from infraprism import AsyncInfraPrismOpenAI
import asyncio
async def main():
client = AsyncInfraPrismOpenAI()
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
entity_type="customer",
entity_id="acme-corp",
)
print(response.choices[0].message.content)
asyncio.run(main())
Function Calling
Function/tool calling works exactly as expected:
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
tools=tools,
entity_type="customer",
entity_id="acme-corp",
)
Privacy Note: Tool definitions and function calls are not sent to InfraPrism—only token counts and costs.
Vision (Image Inputs)
GPT-4 Vision is supported:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
]
}
],
entity_type="customer",
entity_id="acme-corp",
)
Privacy Note: Images are never sent to InfraPrism. We only track token counts.
Embeddings
Track embedding costs:
response = client.embeddings.create(
model="text-embedding-3-small",
input="The quick brown fox",
entity_type="project",
entity_id="search-index",
)
Completions (Legacy)
The legacy completions API is also supported:
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="Once upon a time",
max_tokens=100,
entity_type="project",
entity_id="story-generator",
)
Error Handling
Errors from OpenAI are passed through unchanged:
from openai import OpenAIError
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
entity_type="customer",
entity_id="acme-corp",
)
except OpenAIError as e:
print(f"OpenAI error: {e}")
Failed requests are not tracked (no cost was incurred).
Configuration
See Configuration for all available options.
client = InfraPrismOpenAI(
api_key="sk-...", # OpenAI key
infraprism_api_key="ip-...", # InfraPrism key
infraprism_environment="production",
)
Next Steps
- Anthropic Provider - Track Claude model costs
- Azure OpenAI - Use with Azure deployments
- Cost Attribution - Tagging best practices