Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/content/docs/configuration/providers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ type = "open_ai"
api_key = "${OPENROUTER_API_KEY}"
base_url = "https://openrouter.ai/api/v1"

# OpenRouter-specific headers
[providers.openrouter.headers]
HTTP-Referer = "https://myapp.example.com"
X-Title = "My Application"
# App attribution headers are sent automatically:
# HTTP-Referer: https://hadriangateway.com
# X-OpenRouter-Title: Hadrian Gateway
# Override to customize, or set to "" to opt out:
# [providers.openrouter.headers]
# HTTP-Referer = "https://myapp.example.com"
# X-OpenRouter-Title = "My Application"
```

**Ollama** (local, no API key needed):
Expand Down
4 changes: 2 additions & 2 deletions src/bin/record_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1919,8 +1919,8 @@ async fn record_fixture(
// Add OpenRouter-specific headers
if def.provider == "openrouter" {
request = request
.header("HTTP-Referer", "https://github.com/ScriptSmith/hadrian")
.header("X-Title", "Hadrian Gateway Fixture Recording");
.header("HTTP-Referer", "https://hadriangateway.com")
.header("X-OpenRouter-Title", "Hadrian Gateway");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/config/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ pub struct OpenAiProviderConfig {
pub model_aliases: HashMap<String, String>,

/// Custom headers to include in requests.
/// Useful for provider-specific headers like OpenRouter's HTTP-Referer.
/// For OpenRouter providers, `HTTP-Referer` and `X-OpenRouter-Title` are set
/// automatically for app attribution. Override here to customize or opt out.
#[serde(default)]
pub headers: HashMap<String, String>,

Expand Down
38 changes: 30 additions & 8 deletions src/providers/open_ai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,25 @@ impl OpenAICompatibleProvider {
registry: &CircuitBreakerRegistry,
) -> Self {
let circuit_breaker = registry.get_or_create(provider_name, &config.circuit_breaker);
let base_url = config.base_url.trim_end_matches('/').to_string();

let mut headers = config.headers.clone();

// OpenRouter app attribution: send Hadrian metadata by default unless
// the user has explicitly set these headers (opt-out by overriding).
if base_url.contains("openrouter.ai") {
headers
.entry("HTTP-Referer".to_string())
.or_insert_with(|| "https://hadriangateway.com".to_string());
headers
.entry("X-OpenRouter-Title".to_string())
.or_insert_with(|| "Hadrian Gateway".to_string());
}

Self {
api_key: config.api_key.clone(),
base_url: config.base_url.trim_end_matches('/').to_string(),
headers: config.headers.clone(),
base_url,
headers,
timeout: Duration::from_secs(config.timeout_secs),
retry: config.retry.clone(),
circuit_breaker_config: config.circuit_breaker.clone(),
Expand All @@ -86,9 +100,13 @@ impl OpenAICompatibleProvider {
request
};

let request = self.headers.iter().fold(request, |req, (key, value)| {
req.header(key.as_str(), value.as_str())
});
let request = self
.headers
.iter()
.filter(|(_, value)| !value.is_empty())
.fold(request, |req, (key, value)| {
req.header(key.as_str(), value.as_str())
});

request.timeout(self.timeout)
}
Expand All @@ -108,9 +126,13 @@ impl OpenAICompatibleProvider {
request
};

let request = self.headers.iter().fold(request, |req, (key, value)| {
req.header(key.as_str(), value.as_str())
});
let request = self
.headers
.iter()
.filter(|(_, value)| !value.is_empty())
.fold(request, |req, (key, value)| {
req.header(key.as_str(), value.as_str())
});

request.timeout(self.timeout)
}
Expand Down
Loading