OpenAI with LangSpend (Python)

← Back to Docs

Track OpenAI API costs with zero-code attribution in Python

Learn how to track OpenAI API costs with LangSpend in Python. Works with all OpenAI models including GPT-5, GPT-4 and all the other OpenAI models.

Installation

Install required packages

Using pip:

terminal

Using uv:

terminal

Or install with OpenAI extras:

terminal

Basic Setup

Wrap your OpenAI client

lib/openai_client.py

Chat Completions

GPT-5 Example with Customer & Feature Tracking

💡 Custom Metadata: Use the meta property in customer and feature dictionaries to pass any custom properties for advanced tracking and analytics.

chat.py