diff --git a/docs/configuration.md b/docs/configuration.md index f1b0dcc8c..9f7f41045 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -46,6 +46,8 @@ RubyLLM.configure do |config| # Provide keys ONLY for the providers you intend to use. # Using environment variables (ENV.fetch) is highly recommended. config.openai_api_key = ENV.fetch('OPENAI_API_KEY', nil) + config.openai_organization_id = ENV.fetch('OPENAI_ORGANIZATION_ID', nil) + config.openai_project_id = ENV.fetch('OPENAI_PROJECT_ID', nil) config.anthropic_api_key = ENV.fetch('ANTHROPIC_API_KEY', nil) config.gemini_api_key = ENV.fetch('GEMINI_API_KEY', nil) config.deepseek_api_key = ENV.fetch('DEEPSEEK_API_KEY', nil) @@ -117,6 +119,15 @@ end This setting redirects requests made with `provider: :openai` to your specified base URL. See the [Working with Models Guide]({% link guides/models.md %}#connecting-to-custom-endpoints--using-unlisted-models) for more details on using custom models with this setting. +## Optional OpenAI Headers + +OpenAI supports additional headers for organization and project management: + +* `openai_organization_id`: Specifies the billing organization for API usage when multiple organizations are accessible. +* `openai_project_id`: Tracks API usage for a project. + +These headers are optional and only need to be set if you want to use organization or project-specific billing. + ## Default Models These settings determine which models are used by the top-level helper methods (`RubyLLM.chat`, `RubyLLM.embed`, `RubyLLM.paint`) when no specific `model:` argument is provided. diff --git a/lib/ruby_llm/configuration.rb b/lib/ruby_llm/configuration.rb index 06abbec5e..3e605dd3f 100644 --- a/lib/ruby_llm/configuration.rb +++ b/lib/ruby_llm/configuration.rb @@ -13,6 +13,8 @@ class Configuration # Provider-specific configuration attr_accessor :openai_api_key, :openai_api_base, + :openai_organization_id, + :openai_project_id, :anthropic_api_key, :gemini_api_key, :deepseek_api_key, @@ -56,7 +58,7 @@ def initialize def inspect redacted = lambda do |name, value| - if name.match?(/_key|_secret|_token$/) + if name.match?(/_id|_key|_secret|_token$/) value.nil? ? 'nil' : '[FILTERED]' else value diff --git a/lib/ruby_llm/providers/openai.rb b/lib/ruby_llm/providers/openai.rb index 3dde96b9a..7ad39d9c1 100644 --- a/lib/ruby_llm/providers/openai.rb +++ b/lib/ruby_llm/providers/openai.rb @@ -34,8 +34,10 @@ def api_base(config) def headers(config) { - 'Authorization' => "Bearer #{config.openai_api_key}" - } + 'Authorization' => "Bearer #{config.openai_api_key}", + 'OpenAI-Organization' => config.openai_organization_id, + 'OpenAI-Project' => config.openai_project_id + }.compact end def capabilities