@@ -65,6 +65,7 @@ Stream text with GPT-4, transcribe and translate audio with Whisper, or create i
6565 - [ Translate] ( #translate )
6666 - [ Transcribe] ( #transcribe )
6767 - [ Speech] ( #speech )
68+ - [ Usage] ( #usage )
6869 - [ Errors] ( #errors-1 )
6970 - [ Development] ( #development )
7071 - [ Release] ( #release )
@@ -1460,6 +1461,51 @@ File.binwrite('demo.mp3', response)
14601461# => mp3 file that plays: "This is a speech test!"
14611462```
14621463
1464+ ### Usage
1465+ The Usage API provides information about the consumption of various OpenAI services within your organization. You can retrieve usage data for different endpoints and time periods.
1466+
1467+ ``` ruby
1468+ one_day_ago = Time .now.to_i - 86_400
1469+
1470+ # Retrieve costs data
1471+ response = client.usage.costs(parameters: { start_time: one_day_ago })
1472+ response[" data" ].each do |bucket |
1473+ bucket[" results" ].each do |result |
1474+ puts " #{ Time .at(bucket[" start_time" ]).to_date} : $#{ result.dig(" amount" , " value" ).round(2 ) } "
1475+ end
1476+ end
1477+ => 2025 - 02 - 09 : $0 .0
1478+ => 2025 - 02 - 10 : $0 .42
1479+
1480+ # Retrieve completions usage data
1481+ response = client.usage.completions(parameters: { start_time: one_day_ago })
1482+ puts response[" data" ]
1483+
1484+ # Retrieve embeddings usage data
1485+ response = client.usage.embeddings(parameters: { start_time: one_day_ago })
1486+ puts response[" data" ]
1487+
1488+ # Retrieve moderations usage data
1489+ response = client.usage.moderations(parameters: { start_time: one_day_ago })
1490+ puts response[" data" ]
1491+
1492+ # Retrieve image generation usage data
1493+ response = client.usage.images(parameters: { start_time: one_day_ago })
1494+ puts response[" data" ]
1495+
1496+ # Retrieve audio speech usage data
1497+ response = client.usage.audio_speeches(parameters: { start_time: one_day_ago })
1498+ puts response[" data" ]
1499+
1500+ # Retrieve audio transcription usage data
1501+ response = client.usage.audio_transcriptions(parameters: { start_time: one_day_ago })
1502+ puts response[" data" ]
1503+
1504+ # Retrieve vector stores usage data
1505+ response = client.usage.vector_stores(parameters: { start_time: one_day_ago })
1506+ puts response[" data" ]
1507+ ```
1508+
14631509### Errors
14641510
14651511HTTP errors can be caught like this:
0 commit comments