feat(llm): pass session user_id to Anthropic via metadata#1336
feat(llm): pass session user_id to Anthropic via metadata#1336
Conversation
There was a problem hiding this comment.
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.Anthropicto accept optionalmetadataand forward it toAsyncAnthropic.messages.create. - Update
kimi_cli.llm.create_llmto passmetadata={"user_id": session_id}to Anthropic when asession_idis 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.
| base_url=provider.base_url, | ||
| api_key=resolved_api_key, | ||
| default_max_tokens=50000, | ||
| metadata={"user_id": session_id} if session_id else None, | ||
| ) |
There was a problem hiding this comment.
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).
| tools=tools_, | ||
| stream=self._stream, | ||
| extra_headers=extra_headers, | ||
| metadata=self._metadata if self._metadata is not None else omit, | ||
| **generation_kwargs, |
There was a problem hiding this comment.
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).
、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.Anthropicnow accepts an optionalmetadataparameter and forwards it tomessages.create.kimi_cli.llm.create_llmnow passesmetadata={"user_id": session_id}when creating the Anthropic provider (ifsession_idis available).Checklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.