anthropics / anthropics/claude-agent-sdk-typescript
[BUG] options.env is still overridden by ~/.claude/settings.json in SDK 0.3.199
- Ngôn ngữ chính
- Shell
- Star
- 1.8k
- Fork
- 226
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
## Description
With `@anthropic-ai/claude-agent-sdk@0.3.199`, environment variables passed through
`query({ options: { env } })` can still be overridden by the `env` block in
`~/.claude/settings.json`.
This appears to reproduce the behavior reported in #217 and #215, even though the
SDK changelog states that a related issue was fixed in version 0.2.73.
## Reproduction
Create the following user-level settings file:
`~/.claude/settings.json`
```json
{
"env": {
"ANTHROPIC_API_KEY": "key-from-settings",
"ANTHROPIC_BASE_URL": "https://settings.example.invalid"
}
}
```
Run the SDK with different values in `options.env`:
```typescript
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Hello",
options: {
settingSources: ["user", "project", "local"],
env: {
...process.env,
ANTHROPIC_API_KEY: "key-from-options",
ANTHROPIC_BASE_URL: "https://options.example.invalid"
}
}
})) {
console.log(message);
}
```
## Expected behavior
The spawned Claude Code process should use the values explicitly supplied through
`options.env`:
```text
ANTHROPIC_API_KEY=key-from-options
ANTHROPIC_BASE_URL=https://options.example.invalid
```
## Actual behavior
The spawned Claude Code process uses the values from:
```text
~/.claude/settings.json
```
As a result, the SDK invocation may send requests to the wrong
`ANTHROPIC_BASE_URL` or authenticate with the wrong `ANTHROPIC_API_KEY`.
This is particularly problematic for applications that select credentials and
Anthropic-compatible endpoints dynamically for each request.
## Impact
This behavior prevents SDK applications from reliably selecting a different API key
and base URL per invocation.
It affects, for example:
- Multi-tenant applications with per-request credentials
- Applications that use different Anthropic-compatible providers
- Applications sharing a machine with an interactive Claude Code installation
- Applications that need to preserve the user's global `settings.json` while using
isolated credentials for SDK sessions
## Environment
- `@anthropic-ai/claude-agent-sdk`: `0.3.199`
- OS: Linux
- Runtime: Node.js
- `settingSources`: `["user", "project", "local"]`
## Related issues
- [#217 — Programmatic options.env does not override env values in ~/.claude/settings.json](https://github.com/anthropics/claude-agent-sdk-typescript/issues/217)
- [#215 — ~/.claude/settings.json always takes precedence of query.option.env](https://github.com/anthropics/claude-agent-sdk-typescript/issues/215)
- [#144 — ANTHROPIC_BASE_URL via env option broken in v0.2.8+](https://github.com/anthropics/claude-agent-sdk-typescript/issues/144)
The changelog for SDK `0.2.73` states:
> Fixed `options.env` being overridden by the `~/.claude/settings.json` env block when not using `user` as a `settingSources` option.
However, the behavior is still reproducible with SDK `0.3.199` when the user settings
source is enabled.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Đánh giá
Issue này chưa được đánh giá.