Skip to main content
GitHub

OpenAI

Auto-instrumentation for OpenAI API.

Risicare automatically instruments the OpenAI Python SDK.

Installation

pip install risicare openai

Basic Usage

import risicare
from openai import OpenAI
 
risicare.init()
 
client = OpenAI()
 
# Automatically traced
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?"}
    ]
)

Supported Methods

MethodTraced
chat.completions.createYes (sync + async)
embeddings.createYes (sync + async)

Streaming

Streaming responses are fully supported:

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a poem"}],
    stream=True
)
 
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

The span completes when the stream is fully consumed.

Async Support

Async clients are automatically instrumented:

from openai import AsyncOpenAI
 
client = AsyncOpenAI()
 
async def main():
    response = await client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )

Function Calling

Tool/function calls are captured with full context:

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather?"}],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "parameters": {"type": "object", "properties": {}}
        }
    }]
)

Risicare captures:

  • Tool definitions
  • Tool calls made by the model
  • Tool call arguments

Captured Attributes

AttributeDescription
gen_ai.systemopenai (or detected provider for compatible APIs)
gen_ai.request.modelRequested model name
gen_ai.response.modelModel name returned by API
gen_ai.response.idResponse ID
gen_ai.request.temperatureSampling temperature
gen_ai.request.max_tokensMax output tokens
gen_ai.request.streamWhether streaming was requested
gen_ai.request.has_toolsWhether tools were provided
gen_ai.usage.prompt_tokensInput tokens
gen_ai.usage.completion_tokensOutput tokens
gen_ai.usage.total_tokensTotal tokens
gen_ai.completion.tool_callsNumber of tool calls made
gen_ai.completion.finish_reasonStop reason
gen_ai.latency_msRequest latency in milliseconds

For embedding calls (embeddings.create), Risicare also captures:

AttributeDescription
gen_ai.operationembeddings
gen_ai.input.countNumber of input texts
gen_ai.response.embeddingsNumber of embeddings returned
gen_ai.response.dimensionsEmbedding dimensions

Cost Tracking

Costs are automatically calculated:

ModelInput (per 1M)Output (per 1M)
gpt-4o$2.50$10.00
gpt-4o-mini$0.15$0.60
gpt-4-turbo$10.00$30.00
gpt-3.5-turbo$0.50$1.50

Disable Content Capture

To disable prompt/completion capture:

risicare.init(trace_content=False)

Next Steps