Repository navigation
feat: optional provider-side request attribution via the OpenAI user field - #2584
Conversation
…r field When config.add_user_to_requests is enabled, chat_completion sends the current command and PR URL in the OpenAI-compatible "user" request field, as a compact JSON string, so provider logs and usage exports can be attributed to a specific PR and command without timestamp correlation. OpenRouter records it as external_user and includes it in the activity export. The context comes from the logging extras set by PRAgent.handle_request, the same mechanism add_litellm_callbacks uses. For openrouter/* models the value is also placed in extra_body, because LiteLLM's OpenRouter transformation does not forward the standard user parameter. Disabled by default: it shares request-attribution data with the model provider, so enabling it is an explicit operator choice. Assisted-by: claude-code/claude-fable-5
PR Summary by QodoOptional provider-side request attribution via OpenAI "user" field
AI Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo
1.
|
Review findings from the PR bot: slicing the serialized JSON at 256 chars could cut through closing quotes and braces, contradicting the docs and the tests that parse the value. The cap now applies to the individual values before serialization (command 30 chars, pr_url 200), with a unit test asserting the result stays parseable and within bounds on an oversized URL. The helper also switches to double quotes per the repository style rule. Assisted-by: claude-code/claude-fable-5
Both review findings addressed in e10f995:
|
|
Code review by qodo was updated up to the latest commit e10f995 |
Review findings from the PR bot on the context capture:
- Cross-request attribution leak: the temporary loguru sink is process-wide,
so under concurrent requests the first captured record could belong to
another request, sending someone else's PR URL as this request's user
field. The probe record is now matched by identity (an object() token
passed through the record extras), so a sink can only ever capture its own
request's context. Covered by a new concurrency test with two interleaved
contexts.
- AttributeError guard: record extras are read with (record.get("extra") or {}).
- The sink is removed in a try/finally, so a logging failure cannot leak it.
- The unsupported-provider debug message drops the inner quotes and fits the
line limit.
Assisted-by: claude-code/claude-fable-5
All four findings from the updated review addressed in 91cbd77:
Full unit suite: 1552 passed. For what it is worth, the same process-wide-sink race exists in |
|
Code review by qodo was updated up to the latest commit 91cbd77 |
|
Thanks @FabrizioCafolla !! |
Why
Requests recorded on the provider side carry no link back to the PR that generated them. Attributing cost or debugging a single PR against a provider's activity log requires timestamp correlation between PR-Agent logs and the provider dashboard, which breaks down on deployments serving many repositories: one PR event alone produces several calls (describe, review, improve, retries, fallbacks).
PR-Agent already knows the command and the PR URL at call time, in the logging context that
PRAgent.handle_requestsets. This PR forwards that information to the provider, opt-in.What
When enabled,
chat_completionsends the current command and PR URL in the OpenAI-compatibleuserrequest field, as a compact JSON string:OpenRouter records it as
external_userin the generation details and in the activity export; other providers that persist the field get the same benefit. When the setting is off, requests are byte-identical to today's (unit-tested).How
The context is read with the same loguru-sink mechanism
add_litellm_callbacksuses, so it works from the webhook servers and from the CLI, with no extra API calls.Where the field goes depends on what LiteLLM supports for the target model, checked at runtime via
litellm.get_supported_openai_params(model):userparameteropenrouter/*extra_body(LiteLLM's OpenRouter transformation drops theuserparameter;extra_bodyreaches the request body verbatim)usersupport (e.g. Gemini, DeepSeek)litellm.drop_paramsThe value is truncated to 256 characters as a defensive bound.
Privacy
Enabling the setting shares the PR URL with the model provider, so it is off by default and documented as an explicit operator choice (
docs/docs/usage-guide/additional_configurations.md).Testing
tests/unittest/test_litellm_request_user_field.py(off-by-default no-op, field set with context,extra_bodyrouting foropenrouter/*, unsupported-provider skip, no-context no-op), mirroring the style oftest_litellm_openrouter_controls.py.external_user.