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

1 Like

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