Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature Request: Add support for per-request custom headers #160

@larskoole

Description

@larskoole

Feature Request

Problem

Currently, the Laravel facade doesn't provide a way to set custom headers for individual requests. Headers can only be set globally when the client is instantiated in the ServiceProvider. This limitation makes it difficult to add request-specific headers for solutions like AI Gateway from Cloudflare: https://developers.cloudflare.com/ai-gateway/configuration/caching/#per-request-caching

Proposed Solution

Add a withHeaders() method that can be chained before accessing resources like chat(), completions(), etc.

Ideal API:

use OpenAI\Laravel\Facades\OpenAI;

$result = OpenAI::withHeaders([
    'x-custom-header' => 'custom-value',
    'cf-aig-skip-cache' => true,
])
->chat()
->create([
    'model' => 'o3',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

Benefits

  • Enables request-specific headers without creating multiple client instances
  • Maintains the clean facade API while adding flexibility
  • Useful for logging, tracing, authentication, and other middleware scenarios

Current Workaround

Users currently need to create separate client instances with custom HTTP clients, which is verbose and doesn't integrate well with the Laravel facade pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions