mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-08-22 08:02:19 +00:00
feat(ai): support 1M context window for Anthropic resources (#7891)
* feat(ai): support 1M context window for Anthropic resources Add `enable_1m_context` boolean field to Anthropic resource configuration. When enabled (and not using Vertex AI), sends the `anthropic-beta: context-1m-2025-08-07` header in both the API proxy layer and the AI agent worker layer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(ai): add serde alias for enable_1M_context DB field name The resource_type schema uses `enable_1M_context` (uppercase M) but serde only matched `enable_1m_context` and `enable1mContext`, causing the field to always deserialize as false. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -169,6 +169,9 @@ struct AIStandardResource {
|
||||
/// Platform for Anthropic API (standard or google_vertex_ai)
|
||||
#[serde(default)]
|
||||
platform: AnthropicPlatform,
|
||||
/// Enable 1M context window for Anthropic
|
||||
#[serde(alias = "enable_1M_context", default)]
|
||||
enable_1m_context: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -197,6 +200,7 @@ struct AIRequestConfig {
|
||||
#[allow(dead_code)]
|
||||
pub aws_secret_access_key: Option<String>,
|
||||
pub platform: AnthropicPlatform,
|
||||
pub enable_1m_context: bool,
|
||||
}
|
||||
|
||||
impl AIRequestConfig {
|
||||
@@ -216,10 +220,12 @@ impl AIRequestConfig {
|
||||
aws_access_key_id,
|
||||
aws_secret_access_key,
|
||||
platform,
|
||||
enable_1m_context,
|
||||
) = match resource {
|
||||
AIResource::Standard(resource) => {
|
||||
let region = resource.region.clone();
|
||||
let platform = resource.platform.clone();
|
||||
let enable_1m_context = resource.enable_1m_context;
|
||||
// Skip get_base_url for Bedrock - it uses SDK directly, not HTTP
|
||||
let base_url = if matches!(provider, AIProvider::AWSBedrock) {
|
||||
String::new()
|
||||
@@ -258,6 +264,7 @@ impl AIRequestConfig {
|
||||
aws_access_key_id,
|
||||
aws_secret_access_key,
|
||||
platform,
|
||||
enable_1m_context,
|
||||
)
|
||||
}
|
||||
AIResource::OAuth(resource) => {
|
||||
@@ -279,6 +286,7 @@ impl AIRequestConfig {
|
||||
None,
|
||||
None,
|
||||
AnthropicPlatform::Standard,
|
||||
false,
|
||||
)
|
||||
}
|
||||
};
|
||||
@@ -293,6 +301,7 @@ impl AIRequestConfig {
|
||||
aws_access_key_id,
|
||||
aws_secret_access_key,
|
||||
platform,
|
||||
enable_1m_context,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -394,6 +403,10 @@ impl AIRequestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
if is_anthropic_sdk && self.enable_1m_context {
|
||||
request = request.header("anthropic-beta", "context-1m-2025-08-07");
|
||||
}
|
||||
|
||||
// Add authentication headers
|
||||
if let Some(api_key) = self.api_key {
|
||||
if is_azure {
|
||||
|
||||
@@ -354,11 +354,16 @@ pub struct AnthropicQueryBuilder {
|
||||
#[allow(dead_code)]
|
||||
provider_kind: AIProvider,
|
||||
platform: AnthropicPlatform,
|
||||
enable_1m_context: bool,
|
||||
}
|
||||
|
||||
impl AnthropicQueryBuilder {
|
||||
pub fn new(provider_kind: AIProvider, platform: AnthropicPlatform) -> Self {
|
||||
Self { provider_kind, platform }
|
||||
pub fn new(
|
||||
provider_kind: AIProvider,
|
||||
platform: AnthropicPlatform,
|
||||
enable_1m_context: bool,
|
||||
) -> Self {
|
||||
Self { provider_kind, platform, enable_1m_context }
|
||||
}
|
||||
|
||||
fn is_vertex(&self) -> bool {
|
||||
@@ -582,10 +587,14 @@ impl QueryBuilder for AnthropicQueryBuilder {
|
||||
vec![("Authorization", format!("Bearer {}", api_key))]
|
||||
} else {
|
||||
// Standard Anthropic API uses x-api-key and anthropic-version header
|
||||
vec![
|
||||
let mut headers = vec![
|
||||
("x-api-key", api_key.to_string()),
|
||||
("anthropic-version", ANTHROPIC_VERSION_STANDARD.to_string()),
|
||||
]
|
||||
];
|
||||
if self.enable_1m_context {
|
||||
headers.push(("anthropic-beta", "context-1m-2025-08-07".to_string()));
|
||||
}
|
||||
headers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,6 +119,7 @@ pub fn create_query_builder(provider: &ProviderWithResource) -> Box<dyn QueryBui
|
||||
AIProvider::Anthropic => Box::new(AnthropicQueryBuilder::new(
|
||||
provider.kind.clone(),
|
||||
provider.get_platform().clone(),
|
||||
provider.get_enable_1m_context(),
|
||||
)),
|
||||
AIProvider::OpenRouter => Box::new(OpenRouterQueryBuilder::new()),
|
||||
// All other providers use the completion endpoint
|
||||
|
||||
@@ -190,6 +190,9 @@ pub struct ProviderResource {
|
||||
/// Platform for Anthropic API (standard or google_vertex_ai)
|
||||
#[serde(default)]
|
||||
pub platform: AnthropicPlatform,
|
||||
/// Enable 1M context window for Anthropic
|
||||
#[serde(alias = "enable_1M_context", default)]
|
||||
pub enable_1m_context: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -232,6 +235,10 @@ impl ProviderWithResource {
|
||||
pub fn get_platform(&self) -> &AnthropicPlatform {
|
||||
&self.resource.platform
|
||||
}
|
||||
|
||||
pub fn get_enable_1m_context(&self) -> bool {
|
||||
self.resource.enable_1m_context
|
||||
}
|
||||
}
|
||||
|
||||
/// Token usage information from the AI provider
|
||||
|
||||
Reference in New Issue
Block a user