Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(llm): apply llm_base_url to anthropic and openai providers

lhzapata1ff8c618d736
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 59954396..a24de7fd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,16 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
+### Fixed
+- Fixed an explicit `llm_base_url` (or `AI_MEMORY_LLM_BASE_URL`) being dropped
+ for the `anthropic` and `openai` providers. Config resolved the override for
+ every provider, as documented, but the provider factory never applied it to
+ those two, so requests and the API key went straight to api.anthropic.com or
+ api.openai.com with nothing in the log. A gateway or reverse proxy set in front
+ of either vendor now receives the traffic. The docs now name the backends that
+ take no override: the ChatGPT backend behind `openai-oauth` and `codex`, and
+ `cursor`'s CLI. (#1347)
+
## [3.1.0] - 2026-10-16
### Added
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index 39ac68c8..d924a019 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -3134,9 +3134,10 @@ impl Config {
/// Base URL for `provider`, from the two sources that can supply one.
///
/// An explicit ai-memory setting (`llm_base_url` in `config.toml`, or
- /// `AI_MEMORY_LLM_BASE_URL`) configures any provider: naming ai-memory is
- /// how an operator says they mean it, and proxying a vendor endpoint is a
- /// legitimate deployment.
+ /// `AI_MEMORY_LLM_BASE_URL`) configures every HTTP API provider: naming
+ /// ai-memory is how an operator says they mean it, and proxying a vendor
+ /// endpoint is a legitimate deployment. The ChatGPT backend
+ /// (`openai-oauth`, `codex`) and `cursor`'s CLI take no override.
///
/// The bare `LLM_BASE_URL` is different in kind — a cross-tool convention
/// an operator exports once for a local Ollama and forgets. It reaches
diff --git a/crates/ai-memory-llm/src/factory.rs b/crates/ai-memory-llm/src/factory.rs
index 451f4527..91c59fc8 100644
--- a/crates/ai-memory-llm/src/factory.rs
+++ b/crates/ai-memory-llm/src/factory.rs
@@ -384,8 +384,12 @@ pub fn build_provider(config: ProviderConfig) -> LlmResult<Arc<dyn LlmProvider>>
match config.provider {
ProviderChoice::Anthropic => {
let key = config.auth.require_api_key()?;
+ let mut provider = AnthropicProvider::new(key, config.model)?;
+ if let Some(url) = config.base_url {
+ provider = provider.with_base_url(url);
+ }
Ok(Arc::new(
- AnthropicProvider::new(key, config.model)?
+ provider
.with_timeout_secs(timeout)
.with_reasoning_effort(config.reasoning_effort)
.with_extra_headers(extra_headers),
@@ -393,8 +397,12 @@ pub fn build_provider(config: ProviderConfig) -> LlmResult<Arc<dyn LlmProvider>>
}
ProviderChoice::OpenAi => {
let key = config.auth.require_api_key()?;
+ let mut provider = OpenAiProvider::new(key, config.model)?;
+ if let Some(url) = config.base_url {
+ provider = provider.with_base_url(url);
+ }
Ok(Arc::new(
- OpenAiProvider::new(key, config.model)?
+ provider
.with_timeout_secs(timeout)
.with_reasoning_effort(config.reasoning_effort)
.with_extra_headers(extra_headers),
diff --git a/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs b/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs
index b78c700b..70bc57cb 100644
--- a/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs
+++ b/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs
@@ -231,6 +231,89 @@ async fn an_operator_base_url_redirects_the_opencode_provider() {
assert_eq!(values(request, "user-agent"), vec![DEFAULT_USER_AGENT]);
}
+/// An explicit `llm_base_url` configures the vendor-fixed providers too: it is
+/// how an operator routes `anthropic` or `openai` through a gateway on
+/// purpose. Dropping it sends the request, and the key, straight to the vendor
+/// with nothing in the log.
+#[tokio::test]
+async fn an_operator_base_url_redirects_the_anthropic_provider() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/v1/messages"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "ok"}],
+ "usage": {"input_tokens": 1, "output_tokens": 1},
+ })))
+ .mount(&server)
+ .await;
+
+ let provider = build_provider(ProviderConfig {
+ provider: ProviderChoice::Anthropic,
+ model: "claude-haiku-4-5".into(),
+ auth: ProviderAuth::required_api_key_from_env(
+ "ANTHROPIC_API_KEY",
+ Some(SecretString::from("sk-ant-test")),
+ ),
+ base_url: Some(server.uri()),
+ compat_strict: false,
+ compat_disable_thinking: false,
+ request_timeout_secs: 30,
+ reasoning_effort: None,
+ extra_headers: ExtraHeaders::default(),
+ })
+ .expect("provider builds");
+
+ provider
+ .complete(ChatRequest::user_prompt("hi"))
+ .await
+ .expect("the override must reach the mock, not api.anthropic.com");
+
+ let received = server
+ .received_requests()
+ .await
+ .expect("mock recorded requests");
+ assert_eq!(received.len(), 1, "request did not reach the override");
+}
+
+/// Same contract for `openai`, whose default is api.openai.com.
+#[tokio::test]
+async fn an_operator_base_url_redirects_the_openai_provider() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/v1/chat/completions"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(ok_body()))
+ .mount(&server)
+ .await;
+
+ let provider = build_provider(ProviderConfig {
+ provider: ProviderChoice::OpenAi,
+ model: "gpt-5.4-mini".into(),
+ auth: ProviderAuth::required_api_key_from_env(
+ "OPENAI_API_KEY",
+ Some(SecretString::from("sk-test")),
+ ),
+ base_url: Some(server.uri()),
+ compat_strict: false,
+ compat_disable_thinking: false,
+ request_timeout_secs: 30,
+ reasoning_effort: None,
+ extra_headers: ExtraHeaders::default(),
+ })
+ .expect("provider builds");
+
+ provider
+ .complete(ChatRequest::user_prompt("hi"))
+ .await
+ .expect("the override must reach the mock, not api.openai.com");
+
+ let received = server
+ .received_requests()
+ .await
+ .expect("mock recorded requests");
+ assert_eq!(received.len(), 1, "request did not reach the override");
+}
+
/// The load-bearing claim of layering operator headers over the provider's
/// own defaults: an explicit `AI_MEMORY_LLM_HEADERS` entry must beat the
/// per-operation id, and must arrive exactly once. Appending instead of
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index b5291550..ce0816a3 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -1307,8 +1307,10 @@ ANTHROPIC_API_KEY / OPENAI_API_KEY / GEMINI_API_KEY / LLM_API_KEY
AI_MEMORY_LLM_BASE_URL required for openai-compat (Ollama, vLLM); optional override for
opencode (defaults to the Go endpoint, set
https://opencode.ai/zen/v1 for Zen's catalogue).
- Applies to any provider: naming ai-memory is how an
- operator says a vendor endpoint is proxied on purpose
+ Applies to every HTTP API provider: naming ai-memory
+ is how an operator says a vendor endpoint is proxied
+ on purpose. The ChatGPT backend (openai-oauth, codex)
+ and cursor's CLI have no override
LLM_BASE_URL the unprefixed cross-tool convention, accepted for
openai-compat and opencode only. Providers with a
fixed vendor endpoint (anthropic, openai, gemini,