Skip to content

OpenAI (Python)

Behavior

  • OpenAI responses include token counts; no cost. RunForge server computes authoritative cost from registry.

Install

pip install openai

Minimal example

from runforge import RunForge
from openai import OpenAI
import os

rf = RunForge(api_key=os.environ['RUNFORGE_API_KEY'], endpoint=os.environ.get('RUNFORGE_ENDPOINT','http://localhost:3000/api/ingest'), project_id=os.environ.get('RUNFORGE_PROJECT_ID'))
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])

rf.track({"model": "gpt-4o-mini", "experiment": "openai-demo"}, lambda: client.chat.completions.create(model='gpt-4o-mini', messages=[{"role":"user","content":"hi"}]))

Streaming

  • When available, prefer streaming with final usage included; otherwise accumulate output and send one ingest event.

Troubleshooting

  • Keys: OPENAI_API_KEY, RUNFORGE_API_KEY, RUNFORGE_PROJECT_ID.
  • Ensure server‑side execution to avoid exposing secrets.