@grafctory/llms
    Preparing search index...

    Function createOpenAI

    • Creates an OpenAI API client paced against the published rate quotas.

      The client exposes the full OpenAI surface, with overloads, streaming and response helpers unaltered. Every request naming a model is paced by a per-model adaptive throttle, so a long run keeps within the published rate quota instead of failing on the 429 errors the SDK neither paces nor recovers from.

      Parameters

      • options: ClientOptions & { labels?: Lazy<Labels> } = {}

        The client options, as accepted by the OpenAI constructor; if omitted, the SDK defaults apply, reading the API key from the environment. A custom fetch is retained and issues every request, paced or not, so transport customisations keep applying

        • labels

          The labels identifying the cost source of the requests the client issues, either fixed or computed on each request, so that they can name the job issuing it from the context it runs in; merged with the metadata of the request, which wins on shared keys

        • Optional Readonlylabels?: Lazy<Labels>

          The labels identifying the cost source of the requests the client issues, either fixed or computed on each request, so that they can name the job issuing it from the context it runs in; merged with the metadata of the request, which wins on shared keys

      Returns OpenAI

      A client sharing throttling and logging among all requests naming the same model. Models are told apart by their exact id, so variant spellings of a model are paced independently

      Error If options supplies no credentials and none is published by the environment, as signalled by the SDK through an OpenAIError

      A client is meant to be configured once by the consumer binding it, so that credentials and endpoint are settled for a whole execution rather than repeated by each job that draws on the same models.

      Quota control covers every request naming a model, whichever resource issues it and whatever the SDK version offers. Each model is held to the requests per minute the API states for it in the x-ratelimit-limit-requests header of its responses, and runs as fast as the throttle finds feasible until the first response states it. A request rejected over the rate quota is resumed after the retry-after-ms or retry-after delay the API advertises, or at the pace the throttle settles on if no delay is advertised. A rejection reporting an exhausted account is returned as it stands, as no delay recovers from it. Requests naming no model, such as listing files or models, and requests whose payload cannot be sent twice, such as streamed uploads, are issued unpaced.

      Retrying is left to the SDK for everything but the rate quota, so maxRetries still governs recovery from network failures and server errors exactly as it does on a client created directly through the SDK constructor.

      Every response to a request naming a model, streamed or not, is logged with its model, labels and token usage, one entry per request, so a cost spike can be traced to the job causing it from the log alone: OpenAI billing is split by project and API key alone, never by labels. Output tokens include those spent on reasoning, which are also stated on their own. Usage is requested on streamed chat completions not asking for it, and the chunk carrying it is withheld from the stream, so the caller receives the same chunks it would have received without the request.

      Labels are attached as metadata to response requests and to chat completion requests setting store, as the API keeps metadata only on stored objects and the other endpoints accept none; other requests carry labels in the log alone. The API accepts at most 16 entries, with keys of up to 64 characters and values of up to 512 characters. The client labels, merged with the metadata of the request, are brought within these limits before they are attached, dropping client labels first beyond 16 entries and clipping longer keys and values, and they are logged as supplied.

      Warning

      A client authenticated with X.509 workload identity credentials is served by the SDK transport alone: its requests are neither paced nor logged, and carry no labels beyond the metadata each request states for itself.