Skip to content

feat(llm): pass session user_id to Anthropic via metadata#1336

Merged
RealKai42 merged 3 commits intomainfrom
kaiyi/claude-session-id
Mar 4, 2026
Merged

feat(llm): pass session user_id to Anthropic via metadata#1336
RealKai42 merged 3 commits intomainfrom
kaiyi/claude-session-id

Conversation

@RealKai42
Copy link
Copy Markdown
Collaborator

@RealKai42 RealKai42 commented Mar 4, 2026

、al Anthropic request metadata support in Kosong and wires it in Kimi CLI to include the current session ID as user metadata.`

Changes:

  • kosong.contrib.chat_provider.anthropic.Anthropic now accepts an optional metadata parameter and forwards it to messages.create.
  • kimi_cli.llm.create_llm now passes metadata={"user_id": session_id} when creating the Anthropic provider (if session_id is available).

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

Open with Devin

Copilot AI review requested due to automatic review settings March 4, 2026 10:18
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for Anthropic request metadata in Kosong and wires Kimi CLI session IDs through to Anthropic as metadata.user_id for improved request attribution/tracing.

Changes:

  • Extend kosong.contrib.chat_provider.anthropic.Anthropic to accept optional metadata and forward it to AsyncAnthropic.messages.create.
  • Update kimi_cli.llm.create_llm to pass metadata={"user_id": session_id} to Anthropic when a session_id is available.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/kimi_cli/llm.py Passes session-derived Anthropic metadata during provider construction.
packages/kosong/src/kosong/contrib/chat_provider/anthropic.py Adds metadata parameter to the provider and forwards it into the Messages API request.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 169 to 173
base_url=provider.base_url,
api_key=resolved_api_key,
default_max_tokens=50000,
metadata={"user_id": session_id} if session_id else None,
)
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_llm now conditionally passes Anthropic metadata derived from session_id, but tests/core/test_create_llm.py currently has no coverage for the Anthropic branch (or the session_id→metadata wiring). Add a unit test that calls create_llm(..., session_id="...") with an anthropic provider and asserts the constructed provider carries the expected metadata (or that its outgoing request includes it, if that’s the chosen assertion style).

Copilot uses AI. Check for mistakes.
Comment on lines 225 to 229
tools=tools_,
stream=self._stream,
extra_headers=extra_headers,
metadata=self._metadata if self._metadata is not None else omit,
**generation_kwargs,
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior: when metadata is set it will be forwarded into the Anthropic messages.create request, but the snapshot tests for the Anthropic provider don’t currently assert that metadata is included/omitted correctly. Add a test case that constructs Anthropic(metadata={"user_id": "..."}), calls generate(), and asserts the request body includes the expected metadata (and optionally another case where metadata is omitted when None).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants