Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 17 additions & 4 deletions crates/bedrock/src/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use thiserror::Error;

pub use crate::models::*;

pub const CONTEXT_1M_BETA_HEADER: &str = "context-1m-2025-08-07";

pub async fn stream_completion(
client: bedrock::Client,
request: Request,
Expand All @@ -39,6 +41,8 @@ pub async fn stream_completion(
.model_id(request.model.clone())
.set_messages(request.messages.into());

let mut additional_fields: HashMap<String, Document> = HashMap::new();

if let Some(Thinking::Enabled {
budget_tokens: Some(budget_tokens),
}) = request.thinking
Expand All @@ -50,10 +54,18 @@ pub async fn stream_completion(
Document::Number(AwsNumber::PosInt(budget_tokens)),
),
]);
response = response.additional_model_request_fields(Document::Object(HashMap::from([(
"thinking".to_string(),
Document::from(thinking_config),
)])));
additional_fields.insert("thinking".to_string(), Document::from(thinking_config));
}

if request.allow_extended_context {
additional_fields.insert(
"anthropic_beta".to_string(),
Document::Array(vec![Document::String(CONTEXT_1M_BETA_HEADER.to_string())]),
);
}

if !additional_fields.is_empty() {
response = response.additional_model_request_fields(Document::Object(additional_fields));
}

if request.tools.as_ref().is_some_and(|t| !t.tools.is_empty()) {
Expand Down Expand Up @@ -161,6 +173,7 @@ pub struct Request {
pub temperature: Option<f32>,
pub top_k: Option<u32>,
pub top_p: Option<f32>,
pub allow_extended_context: bool,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
12 changes: 12 additions & 0 deletions crates/bedrock/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,18 @@ impl Model {
}
}

pub fn supports_extended_context(&self) -> bool {
matches!(
self,
Model::ClaudeSonnet4
| Model::ClaudeSonnet4Thinking
| Model::ClaudeSonnet4_5
| Model::ClaudeSonnet4_5Thinking
| Model::ClaudeOpus4_5
| Model::ClaudeOpus4_5Thinking
)
}

pub fn cross_region_inference_id(
&self,
region: &str,
Expand Down
17 changes: 15 additions & 2 deletions crates/language_models/src/provider/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub struct AmazonBedrockSettings {
pub role_arn: Option<String>,
pub authentication_method: Option<BedrockAuthMethod>,
pub allow_global: Option<bool>,
pub allow_extended_context: Option<bool>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, EnumIter, IntoStaticStr, JsonSchema)]
Expand Down Expand Up @@ -377,6 +378,13 @@ impl State {
.and_then(|s| s.allow_global)
.unwrap_or(false)
}

fn get_allow_extended_context(&self) -> bool {
self.settings
.as_ref()
.and_then(|s| s.allow_extended_context)
.unwrap_or(false)
}
}

pub struct BedrockLanguageModelProvider {
Expand Down Expand Up @@ -667,8 +675,8 @@ impl LanguageModel for BedrockModel {
LanguageModelCompletionError,
>,
> {
let (region, allow_global) = cx.read_entity(&self.state, |state, _cx| {
(state.get_region(), state.get_allow_global())
let (region, allow_global, allow_extended_context) = cx.read_entity(&self.state, |state, _cx| {
(state.get_region(), state.get_allow_global(), state.get_allow_extended_context())
});

let model_id = match self.model.cross_region_inference_id(&region, allow_global) {
Expand All @@ -680,13 +688,16 @@ impl LanguageModel for BedrockModel {

let deny_tool_calls = request.tool_choice == Some(LanguageModelToolChoice::None);

let use_extended_context = allow_extended_context && self.model.supports_extended_context();

let request = match into_bedrock(
request,
model_id,
self.model.default_temperature(),
self.model.max_output_tokens(),
self.model.mode(),
self.model.supports_caching(),
use_extended_context,
) {
Ok(request) => request,
Err(err) => return futures::future::ready(Err(err.into())).boxed(),
Expand Down Expand Up @@ -742,6 +753,7 @@ pub fn into_bedrock(
max_output_tokens: u64,
mode: BedrockModelMode,
supports_caching: bool,
allow_extended_context: bool,
) -> Result<bedrock::Request> {
let mut new_messages: Vec<BedrockMessage> = Vec::new();
let mut system_message = String::new();
Expand Down Expand Up @@ -934,6 +946,7 @@ pub fn into_bedrock(
temperature: request.temperature.or(Some(default_temperature)),
top_k: None,
top_p: None,
allow_extended_context,
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/language_models/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl settings::Settings for AllLanguageModelSettings {
role_arn: None, // todo(was never a setting for this...)
authentication_method: bedrock.authentication_method.map(Into::into),
allow_global: bedrock.allow_global,
allow_extended_context: bedrock.allow_extended_context,
},
deepseek: DeepSeekSettings {
api_url: deepseek.api_url.unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions crates/settings_content/src/language_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct AmazonBedrockSettingsContent {
pub profile: Option<String>,
pub authentication_method: Option<BedrockAuthMethodContent>,
pub allow_global: Option<bool>,
/// Enable the 1M token extended context window beta for supported Anthropic models.
pub allow_extended_context: Option<bool>,
}

#[with_fallible_options]
Expand Down
21 changes: 21 additions & 0 deletions docs/src/ai/llm-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ We will support Cross-Region inference for each of the models on a best-effort b

For the most up-to-date supported regions and models, refer to the [Supported Models and Regions for Cross Region inference](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-support.html).

#### Extended Context Window {#bedrock-extended-context}

Anthropic models on Bedrock support a [1M token extended context window](https://docs.anthropic.com/en/docs/build-with-claude/extended-context) beta. To enable this feature, add `"allow_extended_context": true` to your Bedrock configuration:

```json [settings]
{
"language_models": {
"bedrock": {
"authentication_method": "named_profile",
"region": "your-aws-region",
"profile": "your-profile-name",
"allow_extended_context": true
}
}
}
```

When enabled, Zed will include the `anthropic_beta` field in requests to Bedrock, enabling the 1M token context window for supported Anthropic models such as Claude Sonnet 4.5 and Claude Opus 4.6.

> **Note**: Extended context usage may incur additional API costs. Refer to your AWS Bedrock pricing for details.

### Anthropic {#anthropic}

You can use Anthropic models by choosing them via the model dropdown in the Agent Panel.
Expand Down
Loading