Issue With Costs API Pagination – Invalid Page Token Returned

Hi OpenAI Support Team,

My name is Rambabu Rachamalla, and I am a Data Engineer at Fivetran. We are integrating the OpenAI source as part of our connector services, and during this implementation I encountered an issue with the Costs API pagination (endpoint: /v1/organization/costs).

When querying usage costs with pagination, I am receiving a next_page token even when the current page contains no data. The response also returns "has_more": true. However, when I attempt to use the provided next_page token, the API returns a 400 Bad Request with the message:

{
  "error": {
    "message": "The page token is invalid, have you modified the query parameters?",
    "type": "invalid_request_error",
    "code": "invalid_request_error"
  }
}

Here is an example of one such response:

{
  "object": "page",
  "has_more": true,
  "next_page": "page_AAAAAGk9P4XXKcmpAAAAAGWDgAA=",
  "data": [
    {
      "object": "bucket",
      "start_time": 1702944000,
      "end_time": 1703030400,
      "results": []
    },
    {
      "object": "bucket",
      "start_time": 1703030400,
      "end_time": 1703116800,
      "results": []
    }
  ]
}

And here is the request I used:

curl --location 'https://api.openai.com/v1/organization/costs?start_time=1702562400&end_time=1703648800&group_by=project_id%2Cline_item&limit=7' \
--header 'Authorization: Bearer <TOKEN>' \
--header 'accept: application/json'

Even after handling pagination correctly (checking has_more, validating the data array, etc.), the issue persists. It appears the API is returning an unusable pagination token.

Could you please investigate this issue?
If you need more details, I am happy to provide request/response logs.

Thanks
Rambabu Rachamalla

2 Likes

Hi and welcome to the Community!

I am seeing the same issue. At some point, an invalid page token is returned together with the incorrect indication that more pages are available, even though there is no additional data.

Sharing some of the request details below for further investigation:

  • Response 1: 200, has_more=true, next_page=page_AAAAAGk90tdaFLD2AAAAAGjkWAA=, all returned buckets had results: []**
    request_id: req_ee740bc5f3054319ba53f0ac1c91b881

  • Response 2: 400 invalid_request_error when calling same params + page=<next_page>
    request_id: req_a3d955313bd34682acd4fd7a70364ebe

2 Likes

Yes i am also facing same issue could you please check @OpenAI_Support

Hey everyone, Apologies for the inconvenience. We are actively looking into this issue and will provide an update here. Thank you!

2 Likes

Thanks for reporting this. We identified an issue in the Costs API pagination flow where certain parameter changes between paginated requests could cause an `invalid_page_token` error, even when the query was logically equivalent.

A fix for this has been deployed, and the cases we were able to reproduce are now working correctly. Could you retry your request and let us know if you’re still seeing the issue?

If it’s still happening, please open a support request with us at support@openai.com and share:

- the exact request URLs/parameters you’re using for each page

- the request IDs for the failed calls

- a short snippet of how you’re handling pagination in code

That will help us confirm whether you’re hitting a remaining edge case. PLEASE make sure to share the case id/case subject here so we can track your request. Thank you!

2 Likes

Just to add a datapoint, I am still getting the error message. Passing the parameters start_time, end_time, bucket_width: “1d”,group_by: “line_item”, page: and on the first request it’s fine and when using the next_page I always get the error about invalid page token.

1 Like

Thanks for the update. I still can still reproduce the bug though.

Here is what I found:

The current documentation for GET /v1/organization/costs says the next request should use the next_page value from the previous response as the page parameter.

When using grouped Costs queries, the first page returns successfully with has_more=true and a next_page cursor. But when I send that same next_page value in the second request, the API still returns a 400 error saying: The page token is invalid, have you modified the query parameters?

As a comparison, the same endpoint without group_by paginated normally across multiple pages in the same test.

So this does not appear to be fully resolved. From what I observe, the problem seems limited to pagination on grouped /organization/costs queries, rather than pagination on the endpoint in general.

Current workaround: if you need to paginate Costs results, avoid group_by where possible, or keep the grouped query small enough to fit on a single page.

Hope this helps!

2 Likes

On my side, we’ve found that always setting a limit param prevents the error. Previously we always sent a value for

  • start_time
  • end_time
  • bucket_width
  • group_by
  • page

by adding limit to the params we always send, we haven’t had the error. Perhaps on the API side the OpenAI crew is defaulting a limit value but not correctly considering it when they hash for the next_page param?

If the limit param is actually required then I hope OpenAI updates their documentation.

1 Like

Thanks for the report. We identified a few pagination edge cases on this endpoint and have rolled out a fix. Our repro script is now working as expected.

Could you confirm whether this is resolved on your side as well? If you’re still seeing issues, please open a support request at support@openai.com and include the exact API calls you’re making for each page, full URLs/params, request IDs, and a small code snippet showing how pagination is implemented. That’ll help us verify whether you’re hitting one of the same edge cases or something else. Please also share the case ID or case subject in this thread.

2 Likes