usage/
usage.rs

1#![allow(clippy::uninlined_format_args)]
2//! Usage API example.
3//!
4//! This example demonstrates:
5//! - Querying usage data for different API endpoints
6//! - Filtering usage by time range, projects, users, and models
7//! - Aggregating usage data
8//! - Retrieving cost data
9//!
10//! Run with: `cargo run --example usage`
11
12use openai_ergonomic::{
13    builders::usage::{BucketWidth, GroupBy, UsageBuilder},
14    Client, Result,
15};
16
17#[tokio::main]
18async fn main() -> Result<()> {
19    println!("=== Usage API Examples ===\n");
20
21    // Initialize client
22    let client = Client::from_env()?.build();
23
24    // Get current time and time 30 days ago (in Unix timestamp seconds)
25    // Using hardcoded timestamps for demonstration
26    let end_time = 1_700_000_000_i32; // Example: Nov 2023
27    let start_time = end_time - (30 * 24 * 60 * 60); // 30 days ago
28
29    println!(
30        "Querying usage data (timestamps: {} to {})",
31        start_time, end_time
32    );
33    println!();
34
35    // Example 1: Basic usage query
36    println!("1. Basic Usage Query (Completions):");
37    basic_usage_query(&client, start_time, end_time).await?;
38
39    // Example 2: Usage with aggregation
40    println!("\n2. Usage with Daily Aggregation:");
41    usage_with_aggregation(&client, start_time, end_time).await?;
42
43    // Example 3: Usage filtered by model
44    println!("\n3. Usage Filtered by Model:");
45    usage_by_model(&client, start_time, end_time).await?;
46
47    // Example 4: Usage grouped by project
48    println!("\n4. Usage Grouped by Project:");
49    usage_grouped_by_project(&client, start_time, end_time).await?;
50
51    // Example 5: Cost data
52    println!("\n5. Cost Data:");
53    cost_data(&client, start_time, end_time).await?;
54
55    // Example 6: Audio usage
56    println!("\n6. Audio Usage:");
57    audio_usage(&client, start_time, end_time).await?;
58
59    // Example 7: Image usage
60    println!("\n7. Image Usage:");
61    image_usage(&client, start_time, end_time).await?;
62
63    // Example 8: Embeddings usage
64    println!("\n8. Embeddings Usage:");
65    embeddings_usage(&client, start_time, end_time).await?;
66
67    println!("\n=== All examples completed successfully ===");
68
69    Ok(())
70}
71
72async fn basic_usage_query(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
73    let builder = UsageBuilder::new(start_time, Some(end_time));
74
75    let usage = client.usage().completions(builder).await?;
76
77    println!("Completions usage:");
78    println!("  Data points: {}", usage.data.len());
79
80    if usage.has_more {
81        println!("  Has more: yes");
82    }
83
84    Ok(())
85}
86
87async fn usage_with_aggregation(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
88    let builder = UsageBuilder::new(start_time, Some(end_time))
89        .bucket_width(BucketWidth::Day)
90        .limit(10);
91
92    let usage = client.usage().completions(builder).await?;
93
94    println!("Daily aggregated completions usage:");
95    println!("  Bucket width: 1 day");
96    println!("  Data points: {}", usage.data.len());
97
98    Ok(())
99}
100
101async fn usage_by_model(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
102    let builder = UsageBuilder::new(start_time, Some(end_time))
103        .model("gpt-4")
104        .limit(100);
105
106    let usage = client.usage().completions(builder).await?;
107
108    println!("Completions usage for gpt-4:");
109    println!("  Data points: {}", usage.data.len());
110
111    Ok(())
112}
113
114async fn usage_grouped_by_project(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
115    let builder = UsageBuilder::new(start_time, Some(end_time))
116        .group_by(GroupBy::ProjectId)
117        .group_by(GroupBy::Model)
118        .limit(50);
119
120    let usage = client.usage().completions(builder).await?;
121
122    println!("Completions usage grouped by project and model:");
123    println!("  Data points: {}", usage.data.len());
124
125    Ok(())
126}
127
128async fn cost_data(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
129    let builder = UsageBuilder::new(start_time, Some(end_time))
130        .bucket_width(BucketWidth::Day)
131        .limit(10);
132
133    let costs = client.usage().costs(builder).await?;
134
135    println!("Cost data:");
136    println!("  Data points: {}", costs.data.len());
137
138    Ok(())
139}
140
141async fn audio_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
142    let builder = UsageBuilder::new(start_time, Some(end_time)).limit(10);
143
144    // Audio speeches (text-to-speech)
145    let speeches = client.usage().audio_speeches(builder.clone()).await?;
146    println!("Audio speeches usage: {} data points", speeches.data.len());
147
148    // Audio transcriptions
149    let transcriptions = client.usage().audio_transcriptions(builder).await?;
150    println!(
151        "Audio transcriptions usage: {} data points",
152        transcriptions.data.len()
153    );
154
155    Ok(())
156}
157
158async fn image_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
159    let builder = UsageBuilder::new(start_time, Some(end_time))
160        .bucket_width(BucketWidth::Day)
161        .limit(10);
162
163    let usage = client.usage().images(builder).await?;
164
165    println!("Image generation usage:");
166    println!("  Data points: {}", usage.data.len());
167
168    Ok(())
169}
170
171async fn embeddings_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
172    let builder = UsageBuilder::new(start_time, Some(end_time))
173        .model("text-embedding-3-small")
174        .limit(100);
175
176    let usage = client.usage().embeddings(builder).await?;
177
178    println!("Embeddings usage for text-embedding-3-small:");
179    println!("  Data points: {}", usage.data.len());
180
181    Ok(())
182}