Skip to content

fix: record reasoning effort consistently in usage logs - #6641

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/new-api-log
Aug 10, 2026
Merged

fix: record reasoning effort consistently in usage logs#6641
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/new-api-log

Conversation

@seefs001

@seefs001 seefs001 commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

  • 统一OpenAI/OpenAI-Responses/Gemini/Anthropic的reasoning_effort提取
  • 渠道重试保持reasoning_effort正确提取
  • 参数覆盖推理强度增加审计

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • Bug Fixes

    • Improved preservation and synchronization of reasoning-effort settings across supported request formats.
    • Reasoning-effort values now remain accurate after parameter overrides and request retries.
    • Improved handling of missing, removed, blank, and unsupported reasoning-effort values.
  • Usage Logs

    • Standardized reasoning-effort status badges with consistent colors:
      • High effort: orange
      • Medium effort: yellow
      • Low effort: green
      • None or unknown: grey
  • Quality

    • Added comprehensive coverage for reasoning-effort extraction, overrides, retries, and display behavior.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Reasoning effort is now extracted and normalized across supported relay request formats. Parameter overrides keep relay metadata synchronized. Usage-log details use a shared helper to render reasoning-effort badges.

Changes

Reasoning effort propagation

Layer / File(s) Summary
Capture reasoning effort
relay/common/relay_info.go, relay/channel/*/adaptor.go, relay/claude_handler.go, relay/common/relay_info_test.go
Relay metadata extracts reasoning effort from supported formats. Channel handlers use SetReasoningEffort, which trims whitespace. Tests cover extraction and retry restoration.
Synchronize parameter overrides
relay/common/override.go, relay/common/override_test.go
Parameter overrides audit reasoning paths and synchronize relay metadata after updates, deletion, and non-string values.
Render usage-log badges
web/src/features/usage-logs/lib/format.ts, web/src/features/usage-logs/components/dialogs/details-dialog.tsx
A shared helper maps reasoning-effort values to badge variants, and the details dialog uses it.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: calcium-ion

Poem

A rabbit hops through relay streams,
Trimming effort from request dreams.
Overrides keep the state in line,
Retry paths restore the sign.
Logs glow green, yellow, orange, grey.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: consistent reasoning-effort recording in usage logs.
Linked Issues check ✅ Passed The PR extracts and displays reasoning effort across supported formats, including retries and overrides, satisfying the logging objective in [#6634].
Out of Scope Changes check ✅ Passed All changes support consistent reasoning-effort extraction, auditing, retry handling, or usage-log display; no unrelated scope is evident.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@relay/common/override.go`:
- Around line 226-253: Update extractReasoningEffortFromJSON to skip
whitespace-only string values and continue checking later paths, allowing OpenAI
reasoning_effort to fall back to reasoning.effort. Preserve exists=true when
aliases are present but all string values are empty, and add the mixed
blank-top-level/fallback case to
TestApplyParamOverrideWithRelayInfoSynchronizesReasoningEffort.

In `@relay/common/relay_info_test.go`:
- Around line 160-178: Update
TestInitChannelMetaRestoresRequestReasoningEffortForRetry to explicitly
initialize pass-through settings: set the global PassThroughRequestEnabled value
to false and restore its original value with t.Cleanup, and attach
ContextKeyChannelSetting to the request context with PassThroughBodyEnabled set
to false before calling InitChannelMeta.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8c8a9db9-b857-4336-9abe-f58d0dfd3b6a

📥 Commits

Reviewing files that changed from the base of the PR and between 0ab0202 and 8fc42ea.

📒 Files selected for processing (10)
  • relay/channel/deepseek/adaptor.go
  • relay/channel/openai/adaptor.go
  • relay/channel/xai/adaptor.go
  • relay/claude_handler.go
  • relay/common/override.go
  • relay/common/override_test.go
  • relay/common/relay_info.go
  • relay/common/relay_info_test.go
  • web/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • web/src/features/usage-logs/lib/format.ts

Comment thread relay/common/override.go
Comment on lines +226 to +253
func extractReasoningEffortFromJSON(format types.RelayFormat, data []byte) (string, bool) {
var paths []string
switch format {
case types.RelayFormatOpenAI:
paths = []string{"reasoning_effort", "reasoning.effort"}
case types.RelayFormatOpenAIResponses:
paths = []string{"reasoning.effort"}
case types.RelayFormatClaude:
paths = []string{"output_config.effort"}
case types.RelayFormatGemini:
paths = []string{
"generationConfig.thinkingConfig.thinkingLevel",
"generation_config.thinking_config.thinking_level",
}
default:
return "", false
}
for _, path := range paths {
value := gjson.GetBytes(data, path)
if !value.Exists() {
continue
}
if value.Type != gjson.String {
return "", true
}
return strings.TrimSpace(value.String()), true
}
return "", false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Fall back to reasoning.effort after a blank reasoning_effort.

Line 251 returns a blank top-level alias before it checks later aliases. reasoningEffortFromRequest treats a whitespace-only OpenAI reasoning_effort as absent and falls back to reasoning.effort.

An unrelated parameter override can therefore reset RelayInfo.ReasoningEffort from "high" to "" for {"reasoning_effort":" ","reasoning":{"effort":"high"}}. Continue after empty string values, while retaining exists=true when every string alias is empty. Add this case to TestApplyParamOverrideWithRelayInfoSynchronizesReasoningEffort.

Proposed fix
 func extractReasoningEffortFromJSON(format types.RelayFormat, data []byte) (string, bool) {
 	var paths []string
+	foundEmptyString := false
 	switch format {
@@
 		if value.Type != gjson.String {
 			return "", true
 		}
-		return strings.TrimSpace(value.String()), true
+		effort := strings.TrimSpace(value.String())
+		if effort == "" {
+			foundEmptyString = true
+			continue
+		}
+		return effort, true
 	}
-	return "", false
+	return "", foundEmptyString
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func extractReasoningEffortFromJSON(format types.RelayFormat, data []byte) (string, bool) {
var paths []string
switch format {
case types.RelayFormatOpenAI:
paths = []string{"reasoning_effort", "reasoning.effort"}
case types.RelayFormatOpenAIResponses:
paths = []string{"reasoning.effort"}
case types.RelayFormatClaude:
paths = []string{"output_config.effort"}
case types.RelayFormatGemini:
paths = []string{
"generationConfig.thinkingConfig.thinkingLevel",
"generation_config.thinking_config.thinking_level",
}
default:
return "", false
}
for _, path := range paths {
value := gjson.GetBytes(data, path)
if !value.Exists() {
continue
}
if value.Type != gjson.String {
return "", true
}
return strings.TrimSpace(value.String()), true
}
return "", false
func extractReasoningEffortFromJSON(format types.RelayFormat, data []byte) (string, bool) {
var paths []string
foundEmptyString := false
switch format {
case types.RelayFormatOpenAI:
paths = []string{"reasoning_effort", "reasoning.effort"}
case types.RelayFormatOpenAIResponses:
paths = []string{"reasoning.effort"}
case types.RelayFormatClaude:
paths = []string{"output_config.effort"}
case types.RelayFormatGemini:
paths = []string{
"generationConfig.thinkingConfig.thinkingLevel",
"generation_config.thinking_config.thinking_level",
}
default:
return "", false
}
for _, path := range paths {
value := gjson.GetBytes(data, path)
if !value.Exists() {
continue
}
if value.Type != gjson.String {
return "", true
}
effort := strings.TrimSpace(value.String())
if effort == "" {
foundEmptyString = true
continue
}
return effort, true
}
return "", foundEmptyString
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@relay/common/override.go` around lines 226 - 253, Update
extractReasoningEffortFromJSON to skip whitespace-only string values and
continue checking later paths, allowing OpenAI reasoning_effort to fall back to
reasoning.effort. Preserve exists=true when aliases are present but all string
values are empty, and add the mixed blank-top-level/fallback case to
TestApplyParamOverrideWithRelayInfoSynchronizesReasoningEffort.

Comment on lines +160 to +178
func TestInitChannelMetaRestoresRequestReasoningEffortForRetry(t *testing.T) {
gin.SetMode(gin.TestMode)
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
ctx.Request = httptest.NewRequest("POST", "/v1/responses", nil)
request := &dto.OpenAIResponsesRequest{
Model: "gpt-5.6-sol",
Reasoning: &dto.Reasoning{Effort: "max"},
}
info, err := GenRelayInfo(ctx, types.RelayFormatOpenAIResponses, request, nil)
require.NoError(t, err)

info.SetReasoningEffort("high")
info.InitChannelMeta(ctx)
assert.Equal(t, "max", info.ReasoningEffort)

info.SetReasoningEffort("low")
info.InitChannelMeta(ctx)
assert.Equal(t, "max", info.ReasoningEffort)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Initialize pass-through settings in this retry fixture.

Line 172 calls InitChannelMeta, which reads the global PassThroughRequestEnabled setting. The fixture leaves that setting implicit. A prior test can make Line 173 receive "" instead of "max".

Set global pass-through to false through the test fixture and restore it in t.Cleanup. Set ContextKeyChannelSetting with PassThroughBodyEnabled: false explicitly.

As per coding guidelines, “Initialize database, request context, user group, settings, and cache state explicitly in test fixtures.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@relay/common/relay_info_test.go` around lines 160 - 178, Update
TestInitChannelMetaRestoresRequestReasoningEffortForRetry to explicitly
initialize pass-through settings: set the global PassThroughRequestEnabled value
to false and restore its original value with t.Cleanup, and attach
ContextKeyChannelSetting to the request context with PassThroughBodyEnabled set
to false before calling InitChannelMeta.

Source: Coding guidelines

@Calcium-Ion
Calcium-Ion merged commit eab18a8 into QuantumNous:main Aug 10, 2026
3 of 4 checks passed
latioswang added a commit to trycortexai/new-api that referenced this pull request Aug 10, 2026
* fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset (QuantumNous#6249)

* fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset

The outbound request body is a type-erased io.Reader over BodyStorage, so
net/http cannot derive Request.GetBody (it only does so for *bytes.Reader,
*bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport
cannot transparently retry a request once the body has been written and the
upstream resets the stream with a retryable error (REFUSED_STREAM, or a
connection-level GOAWAY); the relay request then fails with:

    http2: Transport: cannot retry err [...] after Request.Body was written;
    define Request.GetBody to avoid this error

This affects every relay path that goes through DoApiRequest (chat, claude,
gemini, responses, embedding, image, rerank).

BodyStorage (memory and disk) already implements io.Seeker, so replay support
only needed wiring:

- NewOutboundJSONBody additionally returns a getBody that rewinds the storage
  and hands out a fresh non-closing reader. The transport only calls GetBody
  after the previous attempt's body has been abandoned, so the rewind cannot
  race an in-flight read.
- RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside
  UpstreamRequestBodySize by the handlers that build storage-backed bodies.
- applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it
  into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is
  still nil.

Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned
the same already-consumed reader, so any transport-level replay would have
silently sent an empty body, and it clobbered the correct snapshot-based
GetBody that net/http derives from the *bytes.Reader bodies the task adaptors
pass in. For non-replayable bodies GetBody now stays nil, so a retry fails
loudly instead of corrupting the request.

Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets
the first stream with REFUSED_STREAM after the body is written and asserts
the transport transparently retries with the complete body.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(relay): hand out independent readers from GetBody (address review)

Per the http.Request.GetBody contract ("returns a new copy of Body"),
each call must yield a reader with its own cursor. The previous
implementation rewound and reused the shared BodyStorage, so two
consecutive GetBody readers would interfere with each other, and a
replay could disturb the primary body's offset under extreme transport
timing (e.g. attempt N's body write not yet fully abandoned when the
transport builds attempt N+1).

Instead of snapshotting the payload (an extra copy), add
BodyStorage.NewReader, which returns an independent zero-copy reader:

- memory mode: a fresh bytes.Reader over the same immutable backing
  array;
- disk mode: a separate file descriptor over the cache file, so the
  transport closing a replayed body only closes that descriptor.

NewOutboundJSONBody's getBody now simply hands out storage.NewReader,
and once the handler releases the storage, GetBody fails with
ErrStorageClosed instead of replaying stale data.

Tests: interleaved reads across two replay readers and the primary
body each observe exactly their own byte stream, for both the memory
and the disk-backed storage; the existing GetBody and HTTP/2 retry
suites still pass (h2 e2e tests flake-free with -count=20).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(relay): bind replayable metadata on pass-through requests

* fix(relay): reset upstream body metadata between channels

* test(relay): cover replay across retries and channel attempts

* fix(relay): stop following upstream redirects

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>

* refactor(relay): move replay metadata onto request bodies

* Merge commit from fork

* feat(channels): refine fetched model categorization (QuantumNous#6632)

* feat(channels): refine fetched model categorization

* fix: channel category

* fix: hy3 category

* fix: test Claude/Gemini endpoints with native request format (QuantumNous#6698)

* feat(rate-limit): add user critical rate limit middleware for access token and aff transfer routes

* fix: 修复兑换码额度精度损失 (QuantumNous#6685)

* fix: 修复兑换码额度精度损失(QuantumNous#6680)

* fix(redemption): guard update data integrity

* CI: enhance release synchronization workflow with optional file syncing

* fix(ali): stop injecting top_p into requests that omit it (QuantumNous#6674)

* fix(channels): classify Qwen TTS models correctly (QuantumNous#6711)

* feat(channels): add auto-disable-only channel test mode (QuantumNous#6728)

* perf(web): debounce server and large-list searches (QuantumNous#6727)

* fix: record reasoning effort consistently in usage logs (QuantumNous#6641)

* feat(relay): expose user and group context to parameter overrides (QuantumNous#6534)

* fix(ollama): preserve reasoning and tool-call context (QuantumNous#6605)

* fix: backend length validation (QuantumNous#5548)

* feat(billing): highlight matched conditional multipliers in logs (QuantumNous#6561)

* feat(billing): highlight matched conditional multipliers in usage logs

* fix(billing): make request rule tracing stable and type-safe

* fix(web): require confirmation before rotating access token (QuantumNous#6749)

---------

Co-authored-by: Lucas <hepo.lucas@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: CaIon <i@caion.me>
Co-authored-by: RedwindA <128586631+RedwindA@users.noreply.github.com>
Co-authored-by: Seefs <40468931+seefs001@users.noreply.github.com>
Co-authored-by: lihu-001 <lihu9048@gmail.com>
Co-authored-by: ENCHIGO <38551565+ENCHIGO@users.noreply.github.com>
0401lucky pushed a commit to 0401lucky/new-api that referenced this pull request Aug 16, 2026
DayFliggy pushed a commit to DayFliggy/Ren2Hub that referenced this pull request Aug 17, 2026
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
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