File size: 10,759 Bytes
04f1444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Code generated by @openmeter/typespec-go. DO NOT EDIT.

package openmeter

import (
	"encoding/json"
	"fmt"
	"time"
)

// Feature create request.
type CreateFeatureRequest struct {
	// Display name of the resource.
	//
	// Between 1 and 256 characters.
	Name string `json:"name"`
	// Optional description of the resource.
	//
	// Maximum 1024 characters.
	Description *string            `json:"description,omitempty"`
	Labels      *map[string]string `json:"labels,omitempty"`
	Key         string             `json:"key"`
	// The meter that the feature is associated with and based on which usage is
	// calculated. If not specified, the feature is static.
	Meter *FeatureMeterReferenceInput `json:"meter,omitempty"`
	// Optional per-unit cost configuration. Use "manual" for a fixed per-unit cost, or
	// "llm" to look up cost from the LLM cost database based on meter group-by
	// properties.
	UnitCost *FeatureUnitCost `json:"unit_cost,omitempty"`
}

// A capability or billable dimension offered by a provider.
type Feature struct {
	ID string `json:"id"`
	// Display name of the resource.
	//
	// Between 1 and 256 characters.
	Name string `json:"name"`
	// Optional description of the resource.
	//
	// Maximum 1024 characters.
	Description *string           `json:"description,omitempty"`
	Labels      map[string]string `json:"labels,omitempty"`
	// An ISO-8601 timestamp representation of entity creation date.
	CreatedAt time.Time `json:"created_at"`
	// An ISO-8601 timestamp representation of entity last update date.
	UpdatedAt time.Time `json:"updated_at"`
	// An ISO-8601 timestamp representation of entity deletion date.
	DeletedAt *time.Time `json:"deleted_at,omitempty"`
	Key       string     `json:"key"`
	// The meter that the feature is associated with and based on which usage is
	// calculated. If not specified, the feature is static.
	Meter *FeatureMeterReference `json:"meter,omitempty"`
	// Optional per-unit cost configuration. Use "manual" for a fixed per-unit cost, or
	// "llm" to look up cost from the LLM cost database based on meter group-by
	// properties.
	UnitCost *FeatureUnitCost `json:"unit_cost,omitempty"`
}

// Result of a feature cost query.
type FeatureCostQueryResult struct {
	// Start of the queried period.
	From *time.Time `json:"from,omitempty"`
	// End of the queried period.
	To *time.Time `json:"to,omitempty"`
	// The cost data rows.
	Data []FeatureCostQueryRow `json:"data"`
}

// A row in the result of a feature cost query.
type FeatureCostQueryRow struct {
	// The metered usage value for the period.
	Usage Numeric `json:"usage"`
	// The computed cost amount (usage × unit cost). Null when pricing is not available
	// for the given combination of dimensions.
	Cost Nullable[Numeric] `json:"cost"`
	// The currency code of the cost amount.
	Currency string `json:"currency"`
	// Detail message when cost amount is null, explaining why the cost could not be
	// resolved.
	Detail *string `json:"detail,omitempty"`
	// The start of the time bucket the value is aggregated over.
	From time.Time `json:"from"`
	// The end of the time bucket the value is aggregated over.
	To time.Time `json:"to"`
	// The dimensions the value is aggregated over. `subject` and `customer_id` are
	// reserved dimensions.
	Dimensions map[string]string `json:"dimensions"`
}

// Token type for LLM cost lookup.
type FeatureLLMTokenType string

const (
	FeatureLLMTokenTypeInput      FeatureLLMTokenType = "input"
	FeatureLLMTokenTypeOutput     FeatureLLMTokenType = "output"
	FeatureLLMTokenTypeCacheRead  FeatureLLMTokenType = "cache_read"
	FeatureLLMTokenTypeCacheWrite FeatureLLMTokenType = "cache_write"
	FeatureLLMTokenTypeReasoning  FeatureLLMTokenType = "reasoning"
	FeatureLLMTokenTypeRequest    FeatureLLMTokenType = "request"
	FeatureLLMTokenTypeResponse   FeatureLLMTokenType = "response"
)

func (value FeatureLLMTokenType) Valid() bool {
	switch value {
	case FeatureLLMTokenTypeInput, FeatureLLMTokenTypeOutput, FeatureLLMTokenTypeCacheRead, FeatureLLMTokenTypeCacheWrite, FeatureLLMTokenTypeReasoning, FeatureLLMTokenTypeRequest, FeatureLLMTokenTypeResponse:
		return true
	default:
		return false
	}
}

// LLM cost lookup configuration. Each dimension (provider, model, token type) can
// be specified as either a static value or a meter group-by property name
// (mutually exclusive).
type FeatureLLMUnitCost struct {
	// The type discriminator for LLM unit cost.
	Type FeatureUnitCostType `json:"type"`
	// Meter group-by property that holds the LLM provider. Use this when the meter has
	// a group-by dimension for provider. Mutually exclusive with `provider`.
	ProviderProperty *string `json:"provider_property,omitempty"`
	// Static LLM provider value (e.g., "openai", "anthropic"). Use this when the
	// feature tracks a single provider. Mutually exclusive with `provider_property`.
	Provider *string `json:"provider,omitempty"`
	// Meter group-by property that holds the model ID. Use this when the meter has a
	// group-by dimension for model. Mutually exclusive with `model`.
	ModelProperty *string `json:"model_property,omitempty"`
	// Static model ID value (e.g., "gpt-4", "claude-3-5-sonnet"). Use this when the
	// feature tracks a single model. Mutually exclusive with `model_property`.
	Model *string `json:"model,omitempty"`
	// Meter group-by property that holds the token type. Use this when the meter has a
	// group-by dimension for token type. Mutually exclusive with `token_type`.
	TokenTypeProperty *string `json:"token_type_property,omitempty"`
	// Static token type value. Use this when the feature tracks a single token type
	// (e.g., only input tokens). `request` is an alias for `input`, `response` is an
	// alias for `output`. Mutually exclusive with `token_type_property`.
	TokenType *FeatureLLMTokenType `json:"token_type,omitempty"`
	// Resolved per-token pricing from the LLM cost database. Populated in responses
	// when the provider and model can be determined, either from static values or from
	// meter group-by filters with exact matches.
	Pricing *FeatureLLMUnitCostPricing `json:"pricing,omitempty"`
}

// Resolved per-token pricing from the LLM cost database.
type FeatureLLMUnitCostPricing struct {
	// Cost per input token in USD.
	InputPerToken Numeric `json:"input_per_token"`
	// Cost per output token in USD.
	OutputPerToken Numeric `json:"output_per_token"`
	// Cost per cache read token in USD.
	CacheReadPerToken *Numeric `json:"cache_read_per_token,omitempty"`
	// Cost per reasoning token in USD.
	ReasoningPerToken *Numeric `json:"reasoning_per_token,omitempty"`
	// Cost per cache write token in USD.
	CacheWritePerToken *Numeric `json:"cache_write_per_token,omitempty"`
}

// A fixed per-unit cost amount.
type FeatureManualUnitCost struct {
	// The type discriminator for manual unit cost.
	Type FeatureUnitCostType `json:"type"`
	// Fixed per-unit cost amount in USD.
	Amount Numeric `json:"amount"`
}

// Reference to a meter associated with a feature.
type FeatureMeterReference struct {
	// The ID of the meter to associate with this feature.
	ID string `json:"id"`
	// Filters to apply to the dimensions of the meter.
	Filters map[string]QueryFilterStringMapItem `json:"filters,omitempty"`
}

// Reference to a meter associated with a feature.
type FeatureMeterReferenceInput struct {
	// The ID of the meter to associate with this feature.
	ID string `json:"id"`
	// Filters to apply to the dimensions of the meter.
	Filters *map[string]QueryFilterStringMapItemInput `json:"filters,omitempty"`
}

// Page paginated response.
type FeaturePagePaginatedResponse struct {
	Data []Feature     `json:"data"`
	Meta PaginatedMeta `json:"meta"`
}

// Per-unit cost configuration for a feature. Either a fixed manual amount or a
// dynamic LLM cost lookup.
//
// FeatureUnitCost is a JSON-preserving tagged union: its zero value marshals as JSON null, and values must be built with the FeatureUnitCostFrom* constructors.
// The exported Type field is decode-side metadata; MarshalJSON round-trips the original payload and ignores writes to it.
type FeatureUnitCost struct {
	Type string `json:"type"`
	raw  json.RawMessage
}

func (u *FeatureUnitCost) UnmarshalJSON(data []byte) error {
	u.raw = append([]byte(nil), data...)
	if string(data) == "null" {
		u.Type = ""
		return nil
	}

	var envelope struct {
		Value string `json:"type"`
	}
	if err := json.Unmarshal(data, &envelope); err != nil {
		return err
	}
	u.Type = envelope.Value
	return nil
}

func (u FeatureUnitCost) MarshalJSON() ([]byte, error) {
	if len(u.raw) == 0 {
		return []byte("null"), nil
	}
	return append([]byte(nil), u.raw...), nil
}

func (u FeatureUnitCost) AsFeatureManualUnitCost() (*FeatureManualUnitCost, error) {
	if u.Type != "manual" {
		return nil, fmt.Errorf("FeatureUnitCost: expected type %q, got %q", "manual", u.Type)
	}
	var value FeatureManualUnitCost
	if err := json.Unmarshal(u.raw, &value); err != nil {
		return nil, err
	}
	return &value, nil
}

func FeatureUnitCostFromFeatureManualUnitCost(value FeatureManualUnitCost) (FeatureUnitCost, error) {
	value.Type = "manual"
	raw, err := json.Marshal(value)
	if err != nil {
		return FeatureUnitCost{}, err
	}
	var result FeatureUnitCost
	if err := result.UnmarshalJSON(raw); err != nil {
		return FeatureUnitCost{}, err
	}
	return result, nil
}

func (u FeatureUnitCost) AsFeatureLLMUnitCost() (*FeatureLLMUnitCost, error) {
	if u.Type != "llm" {
		return nil, fmt.Errorf("FeatureUnitCost: expected type %q, got %q", "llm", u.Type)
	}
	var value FeatureLLMUnitCost
	if err := json.Unmarshal(u.raw, &value); err != nil {
		return nil, err
	}
	return &value, nil
}

func FeatureUnitCostFromFeatureLLMUnitCost(value FeatureLLMUnitCost) (FeatureUnitCost, error) {
	value.Type = "llm"
	raw, err := json.Marshal(value)
	if err != nil {
		return FeatureUnitCost{}, err
	}
	var result FeatureUnitCost
	if err := result.UnmarshalJSON(raw); err != nil {
		return FeatureUnitCost{}, err
	}
	return result, nil
}

// The type of unit cost.
type FeatureUnitCostType string

const (
	FeatureUnitCostTypeLLM    FeatureUnitCostType = "llm"
	FeatureUnitCostTypeManual FeatureUnitCostType = "manual"
)

func (value FeatureUnitCostType) Valid() bool {
	switch value {
	case FeatureUnitCostTypeLLM, FeatureUnitCostTypeManual:
		return true
	default:
		return false
	}
}

// Request body for updating a feature. Currently only the unit_cost field can be
// updated.
type UpdateFeatureRequest struct {
	// Optional per-unit cost configuration. Use "manual" for a fixed per-unit cost, or
	// "llm" to look up cost from the LLM cost database based on meter group-by
	// properties. Set to `null` to clear the existing unit cost; omit to leave it
	// unchanged.
	UnitCost Nullable[FeatureUnitCost] `json:"unit_cost,omitempty"`
}