OpenAI Integration

Full observability and governance for OpenAI APIs. Track token usage, latency, costs, and ensure quality across GPT-4 and GPT-3.5 deployments.

OpenAI SDK >= 1.0.0GPT-4 / GPT-4oGPT-3.5Embeddings

Installation

Terminal
pip install turingpulse_sdk turingpulse_sdk_openai openai

Quick Start

main.py
from openai import OpenAI
from turingpulse_sdk import init, TuringPulseConfig
from turingpulse_sdk_openai import patch_openai

# Initialize TuringPulse
init(TuringPulseConfig(
    api_key="sk_live_...",
    workflow_name="my-project"
))

# Instrument OpenAI - wraps all API calls
patch_openai()

# Your code works exactly the same - now with full tracing!
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Supported Features

Models

  • GPT-4o / GPT-4o-mini
  • GPT-4 / GPT-4 Turbo
  • GPT-3.5 Turbo
  • o1-preview / o1-mini
  • text-embedding-3
  • DALL·E 3

Capabilities

  • Chat completions
  • Streaming responses
  • Function calling
  • Vision (GPT-4V)
  • Embeddings
  • Assistants API

Tracked Metrics

  • Token usage (in/out)
  • Latency (TTFB, total)
  • Cost estimation
  • Finish reasons
  • Tool calls
  • Error rates

Streaming Support

streaming.py
client = OpenAI()

# Streaming is automatically tracked
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a poem"}],
    stream=True
)

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

# Trace captures time-to-first-token and full response

Function Calling

functions.py
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            }
        }
    }
}]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Weather in NYC?"}],
    tools=tools
)

# Function calls are captured in trace

With KPIs & Alerts

kpis.py
from turingpulse_sdk import instrument, KPIConfig, GovernanceDirective
from turingpulse_sdk_openai import patch_openai

patch_openai(name="openai-service", governance=GovernanceDirective(hatl=True))

@instrument(
    name="openai-agent",
    kpis=[
        KPIConfig(kpi_id="latency_ms", use_duration=True, alert_threshold=5000),
        KPIConfig(kpi_id="cost_usd", from_result_path="cost", alert_threshold=0.10, comparator="gt"),
    ],
)
def my_agent(query: str):
    return client.chat.completions.create(model="gpt-4o", messages=[{"role": "user", "content": query}])
💡
Cost Tracking
TuringPulse automatically calculates costs based on OpenAI pricing. View cost breakdowns by model in the dashboard.

Next Steps