diff --git a/.castiron.stats.yml b/.castiron.stats.yml index 68a09eefad..eafc361c2c 100644 --- a/.castiron.stats.yml +++ b/.castiron.stats.yml @@ -1,7 +1,7 @@ schema_version: 1 -generation_id: 8b821b6c-0836-44bb-a4a9-6f62fdac616e -openapi_spec_hash: 6bb5b5586f1ffada85bccfd43170bfa3 -openapi_transformed_spec_hash: e15abdc5dfeb4d1b3bc07bf2ba10de1e -config_hash: eb083898f75b3c78d03e3c6d075e53d8 -codegen_sha: 3d9c87a0ececa02d59d3755babfce9993e3c0a11 -codegen_hash: 1bd1db260d1cf8172d9ef9d6dc02eb205973e3d245b8126f8a30b1ce91f56abe +generation_id: b0f1a2bb-5d48-43d4-a0b8-f050b37cbbd9 +openapi_spec_hash: a1dc66efb759a0dc5b3b20904545890f +openapi_transformed_spec_hash: 8e506c394d2f27f9329518058d5aa3b9 +config_hash: fb6bc01fab5ad1159ebbfcd9580b679d +codegen_sha: 4a0c1dcef5111e4fdbec4bdbaf957055ec903856 +codegen_hash: fb2cfdf4eba8a2a31de14717d791ea94c01a844969ef43a116c576713d8ace9d diff --git a/api.md b/api.md index 49286927d5..91e22f3420 100644 --- a/api.md +++ b/api.md @@ -1360,11 +1360,11 @@ Types: ```python from openai.types.admin.organization import ( CostQuantityUnit, + UsageCostsResponse, UsageAudioSpeechesResponse, UsageAudioTranscriptionsResponse, UsageCodeInterpreterSessionsResponse, UsageCompletionsResponse, - UsageCostsResponse, UsageDecisionsResponse, UsageEmbeddingsResponse, UsageFileSearchCallsResponse, diff --git a/api_reference/openapi.transformed.yml b/api_reference/openapi.transformed.yml index 7bce3afd61..78da92b994 100644 --- a/api_reference/openapi.transformed.yml +++ b/api_reference/openapi.transformed.yml @@ -11178,7 +11178,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/UsageResponse' + $ref: '#/components/schemas/CostsResponseWithDimensions' '400': description: The request parameters are invalid or the requested operation cannot be performed. content: @@ -11196,6 +11196,7 @@ paths: x-oaiMeta: group: usage-costs examples: + - title: Total costs request: curl: | curl "https://api.openai.com/v1/organization/costs?start_time=1730419200&limit=1" \ @@ -11217,6 +11218,8 @@ paths: "currency": "usd" }, "line_item": null, + "model": null, + "token_type": null, "project_id": null, "api_key_id": null, "quantity": null, @@ -11228,6 +11231,41 @@ paths: "has_more": false, "next_page": null } + - title: Costs by line item + request: + curl: | + curl "https://api.openai.com/v1/organization/costs?start_time=1730419200&limit=1&group_by=line_item" \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: | + { + "object": "page", + "data": [ + { + "object": "bucket", + "start_time": 1730419200, + "end_time": 1730505600, + "results": [ + { + "object": "organization.costs.result", + "amount": { + "value": 0.06, + "currency": "usd" + }, + "line_item": "gpt-6.2-astra, input_tokens", + "model": "gpt-6.2-astra", + "token_type": "input", + "project_id": null, + "api_key_id": null, + "quantity": 10000, + "quantity_unit": "tokens" + } + ] + } + ], + "has_more": false, + "next_page": null + } /organization/data_retention: get: security: @@ -115267,6 +115305,261 @@ components: - $ref: '#/components/schemas/CreateSpendLimitParams' description: The monthly hard spend limit for the initial API key. You cannot set a spend limit when `create_service_account_only` is `true`. - type: 'null' + OrganizationCostAmountModel: + properties: + value: + type: number + currency: + type: string + type: object + required: + - value + - currency + OrganizationCostQuantityUnit: + anyOf: + - type: string + - type: string + enum: + - tokens + - 1000_tokens + - duration_seconds + - duration_minutes + - duration_hours + - gibibyte_hours + - images + - characters + PublicOrganizationCostBucketModel: + properties: + object: + type: string + enum: + - bucket + default: bucket + x-stainless-const: true + start_time: + type: integer + end_time: + type: integer + results: + items: + $ref: '#/components/schemas/PublicOrganizationCostResultModel' + type: array + type: object + required: + - object + - start_time + - end_time + - results + PublicOrganizationCostResultModel: + properties: + api_source: + anyOf: + - type: string + description: API source (agents_api or unlabeled) when grouped by api_source; otherwise null. Unlabeled includes records without published source attribution. + - type: 'null' + object: + type: string + enum: + - organization.costs.result + default: organization.costs.result + x-stainless-const: true + amount: + $ref: '#/components/schemas/OrganizationCostAmountModel' + line_item: + anyOf: + - type: string + - type: 'null' + model: + anyOf: + - type: string + description: The model associated with the billed product, including purchased capacity. This may differ from the model ID used in a request, including fine-tuned model IDs. Null when line_item is null or a model association is unavailable or not applicable. + - type: 'null' + token_type: + anyOf: + - type: string + description: 'The billed token category: `input`, `output`, `input_cached`, `input_cache_write_30m`, `input_cache_write_12h`, or `total`. For inference charges, `input` excludes separately billed cache reads and writes. For Scale Tier purchases, these categories describe purchased capacity. Null when line_item is null or when the category is unknown or inapplicable. Additional values may be introduced in the future.' + - type: 'null' + project_id: + anyOf: + - type: string + - type: 'null' + user_id: + anyOf: + - type: string + - type: 'null' + api_key_id: + anyOf: + - type: string + - type: 'null' + quantity: + anyOf: + - type: number + - type: 'null' + quantity_unit: + anyOf: + - $ref: '#/components/schemas/OrganizationCostQuantityUnit' + - type: 'null' + type: object + required: + - object + - amount + - line_item + - project_id + PublicOrganizationCostsResource: + properties: + object: + type: string + enum: + - page + default: page + x-stainless-const: true + data: + items: + $ref: '#/components/schemas/PublicOrganizationCostBucketModel' + type: array + has_more: + type: boolean + next_page: + anyOf: + - type: string + - type: 'null' + type: object + required: + - object + - data + - has_more + - next_page + CostsResultWithDimensions: + type: object + description: The aggregated costs details of the specific time bucket. + properties: + api_source: + description: When grouped by `api_source`, `agents_api` identifies attributed Agents API activity and `unlabeled` includes all records without published source attribution, including historical and unknown origins. Unlabeled does not imply direct API usage. Without source grouping, this field is null. + anyOf: + - type: string + enum: + - agents_api + - unlabeled + - type: 'null' + object: + type: string + const: organization.costs.result + amount: + type: object + description: The monetary value in its associated currency. + properties: + value: + type: number + description: The numeric value of the cost. + currency: + type: string + description: Lowercase ISO-4217 currency e.g. "usd" + line_item: + anyOf: + - type: string + description: When `group_by=line_item`, this field provides the line item of the grouped costs result. + - type: 'null' + project_id: + anyOf: + - type: string + description: When `group_by=project_id`, this field provides the project ID of the grouped costs result. + - type: 'null' + user_id: + anyOf: + - type: string + description: When `group_by=user_id`, this field provides the user ID of the grouped costs result. + - type: 'null' + api_key_id: + anyOf: + - type: string + description: When `group_by=api_key_id`, this field provides the API Key ID of the grouped costs result. + - type: 'null' + quantity: + anyOf: + - type: number + description: When `group_by=line_item`, this field provides the quantity of the grouped costs result. + - type: 'null' + quantity_unit: + $ref: '#/components/schemas/CostsResult/properties/quantity_unit' + model: + anyOf: + - type: string + description: The model associated with the billed product, including purchased capacity. This may differ from the model ID used in a request, including fine-tuned model IDs. Null when line_item is null or a model association is unavailable or not applicable. + - type: 'null' + token_type: + anyOf: + - type: string + description: 'The billed token category: `input`, `output`, `input_cached`, `input_cache_write_30m`, `input_cache_write_12h`, or `total`. For inference charges, `input` excludes separately billed cache reads and writes. For Scale Tier purchases, these categories describe purchased capacity. Null when line_item is null or when the category is unknown or inapplicable. Additional values may be introduced in the future.' + - type: 'null' + required: + - object + x-oaiMeta: + name: Costs object + example: | + { + "object": "organization.costs.result", + "amount": { + "value": 0.06, + "currency": "usd" + }, + "line_item": "gpt-6.2-astra, input_tokens", + "project_id": "proj_abc", + "quantity": 10000, + "quantity_unit": "tokens" + } + CostsTimeBucketWithDimensions: + type: object + properties: + object: + type: string + const: bucket + start_time: + type: integer + end_time: + type: integer + results: + type: array + items: + discriminator: + propertyName: object + anyOf: + - $ref: '#/components/schemas/UsageCompletionsResult' + - $ref: '#/components/schemas/UsageEmbeddingsResult' + - $ref: '#/components/schemas/UsageModerationsResult' + - $ref: '#/components/schemas/UsageImagesResult' + - $ref: '#/components/schemas/UsageAudioSpeechesResult' + - $ref: '#/components/schemas/UsageAudioTranscriptionsResult' + - $ref: '#/components/schemas/UsageVectorStoresResult' + - $ref: '#/components/schemas/UsageCodeInterpreterSessionsResult' + - $ref: '#/components/schemas/UsageFileSearchCallsResult' + - $ref: '#/components/schemas/UsageWebSearchCallsResult' + - $ref: '#/components/schemas/CostsResultWithDimensions' + required: + - object + - start_time + - end_time + - results + CostsResponseWithDimensions: + type: object + properties: + object: + type: string + const: page + data: + type: array + items: + $ref: '#/components/schemas/CostsTimeBucketWithDimensions' + has_more: + type: boolean + next_page: + anyOf: + - type: string + - type: 'null' + required: + - object + - data + - has_more + - next_page DecisionPromptCacheBreakpoint: type: object properties: diff --git a/scripts/castiron/generated-files.json b/scripts/castiron/generated-files.json index c6eb569f33..2ea75e0805 100644 --- a/scripts/castiron/generated-files.json +++ b/scripts/castiron/generated-files.json @@ -3,7 +3,7 @@ "files": { "api.md": { "mode": "100644", - "sha256": "b39761d9a28a009e155bca1d2ffe6f2692f56e297101edbb85c1df52dbc0066b" + "sha256": "4afd58ab9dca50c1a2ca5543cc8d53cbd6268b72be57264f2b838eb3440d5792" }, "src/openai/__init__.py": { "mode": "100644", @@ -1451,7 +1451,7 @@ }, "src/openai/types/admin/organization/usage_costs_response.py": { "mode": "100644", - "sha256": "18db984905da2334fde6b0edb2ae04f65b7d4ffe4bb58759f1123ce5bb0f16ff" + "sha256": "291441b12df73417c2c1a385c8e2492c801f3760f0bcc75338de721bdeb3552d" }, "src/openai/types/admin/organization/usage_decisions_params.py": { "mode": "100644", diff --git a/src/openai/types/admin/organization/usage_costs_response.py b/src/openai/types/admin/organization/usage_costs_response.py index 98a5df76dd..09585155cf 100644 --- a/src/openai/types/admin/organization/usage_costs_response.py +++ b/src/openai/types/admin/organization/usage_costs_response.py @@ -498,6 +498,14 @@ class DataResultOrganizationCostsResult(BaseModel): costs result. """ + model: Optional[str] = None + """The model associated with the billed product, including purchased capacity. + + This may differ from the model ID used in a request, including fine-tuned model + IDs. Null when line_item is null or a model association is unavailable or not + applicable. + """ + project_id: Optional[str] = None """ When `group_by=project_id`, this field provides the project ID of the grouped @@ -516,6 +524,16 @@ class DataResultOrganizationCostsResult(BaseModel): If no single supported unit applies to the result, this field is `null`. """ + token_type: Optional[str] = None + """ + The billed token category: `input`, `output`, `input_cached`, + `input_cache_write_30m`, `input_cache_write_12h`, or `total`. For inference + charges, `input` excludes separately billed cache reads and writes. For Scale + Tier purchases, these categories describe purchased capacity. Null when + line_item is null or when the category is unknown or inapplicable. Additional + values may be introduced in the future. + """ + user_id: Optional[str] = None """ When `group_by=user_id`, this field provides the user ID of the grouped costs diff --git a/tests/test_costs_dimensions.py b/tests/test_costs_dimensions.py new file mode 100644 index 0000000000..367d3ab36b --- /dev/null +++ b/tests/test_costs_dimensions.py @@ -0,0 +1,56 @@ +from __future__ import annotations + +import httpx2 + +from openai import OpenAI +from openai.types.admin.organization.usage_costs_response import DataResultOrganizationCostsResult + + +def test_costs_dimensions() -> None: + def respond(request: httpx2.Request) -> httpx2.Response: + assert request.method == "GET" + assert request.url.path == "/organization/costs" + assert request.url.params["start_time"] == "0" + return httpx2.Response( + 200, + json={ + "object": "page", + "data": [ + { + "object": "bucket", + "start_time": 0, + "end_time": 60, + "results": [ + { + "object": "organization.costs.result", + "model": "distinct-billed-model", + "token_type": "input_cache_write_30m", + }, + {"object": "organization.costs.result", "model": None, "token_type": None}, + {"object": "organization.costs.result"}, + ], + } + ], + "has_more": False, + "next_page": None, + }, + ) + + with OpenAI( + admin_api_key="fake-admin-key", + base_url="https://example.test", + max_retries=0, + http_client=httpx2.Client(transport=httpx2.MockTransport(respond)), + ) as client: + page = client.admin.organization.usage.costs(start_time=0) + + populated, explicit_null, omitted = page.data[0].results + assert isinstance(populated, DataResultOrganizationCostsResult) + assert isinstance(explicit_null, DataResultOrganizationCostsResult) + assert isinstance(omitted, DataResultOrganizationCostsResult) + assert populated.model == "distinct-billed-model" + assert populated.token_type == "input_cache_write_30m" + assert explicit_null.model is None and explicit_null.token_type is None + assert {"model", "token_type"} <= explicit_null.model_dump(exclude_unset=True).keys() + assert omitted.model is None and omitted.token_type is None + assert {"model", "token_type"}.isdisjoint(omitted.model_dump(exclude_unset=True))