Skip to content

Expose HTTP response headers in KoogHttpClientException for rate limit handling #1354

@jay-anbaric

Description

@jay-anbaric

Currently, KtorKoogHttpClient.processResponse() only captures the body:

  throw KoogHttpClientException(
      clientName = clientName,
      statusCode = response.status.value,
      errorBody = response.bodyAsText(),  // Headers discarded
  )

This forces RetryAfterExtractor implementations to parse retry delays from unstructured error messages, which works for Google Gemini in most cases, but for OpenAI they return rate limit headers, according to https://platform.openai.com/docs/guides/rate-limits.

Rate limits in headers

Field Sample Value Description
x-ratelimit-limit-requests 60 The maximum number of requests that are permitted before exhausting the rate limit.
x-ratelimit-limit-tokens 150000 The maximum number of tokens that are permitted before exhausting the rate limit.
x-ratelimit-remaining-requests 59 The remaining number of requests that are permitted before exhausting the rate limit.
x-ratelimit-remaining-tokens 149984 The remaining number of tokens that are permitted before exhausting the rate limit.
x-ratelimit-reset-requests 1s The time until the rate limit (based on requests) resets to its initial state.
x-ratelimit-reset-tokens 6m0s The time until the rate limit (based on tokens) resets to its initial state.

For Gemini, we actually had to implement a retryAfterExtractor as the standard message extraction did not pick up Gemini’s rate limit window from the message. But for OpenAI (or other providers) that uses standard response headers to signal rate limit, it seems there is no straightforward way to extract this?

koog = "0.6.0"

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestrefactoringCode improvements that enhance structure, readability, and quality without changing functionality

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions