[fix] LLM 재시도 시 토큰이 누적되지 않던 문제 해결 - #67
Conversation
재시도(다중 LLM 호출) 시 성공/최종실패 로그에 한 시도의 토큰만 남겨 재시도로 소모된 토큰이 누락됐다(비용·일일 상한 과소 집계). 시도별 토큰을 합산하는 TokenUsageAccumulator 를 도입해, 모든 시도(성공+실패)의 토큰을 최종 로그에 누적한다.
누산기 단위 테스트와, 1차 실패→2차 성공 및 양쪽 실패 시 두 시도 토큰이 합산 기록되는지 검증하는 서비스 테스트를 추가한다.
WalkthroughLLM 댓글·답글 생성의 재시도별 성공 및 실패 토큰을 Changes재시도 토큰 집계
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant CommentGenerationService
participant LLM
participant GenerationLogRecorder
loop 재시도
CommentGenerationService->>LLM: 생성 요청
LLM-->>CommentGenerationService: 출력 또는 실패 예외
CommentGenerationService->>CommentGenerationService: 토큰 누적
end
CommentGenerationService->>GenerationLogRecorder: 누적 토큰 및 결과 기록
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Test Coverage
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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
`@src/test/kotlin/com/nexters/gamss/conversation/service/CommentGenerationServiceTest.kt`:
- Around line 180-249: CommentGenerationServiceTest의 기존 토큰 누적 검증에 답글 재시도 경로를
추가하세요. generateReplyWithRetry에서 첫 시도 검증 실패 후 두 번째 시도 성공하는 경우, 두 시도의 토큰 합계와 성공
로그를 검증하고, 첫 시도 후 재시도할 수 없는 일반 예외로 즉시 종료되는 경우에도 누적 토큰과 실패 로그가 기록되는지 검증하세요.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 5a51d731-da59-4b78-a1e1-d652821a44c3
📒 Files selected for processing (4)
src/main/kotlin/com/nexters/gamss/conversation/service/CommentGenerationService.ktsrc/main/kotlin/com/nexters/gamss/conversation/service/TokenUsageAccumulator.ktsrc/test/kotlin/com/nexters/gamss/conversation/service/CommentGenerationServiceTest.ktsrc/test/kotlin/com/nexters/gamss/conversation/service/TokenUsageAccumulatorTest.kt
kite707
left a comment
There was a problem hiding this comment.
작업하느라 고생하셨습니다!
처음에는 CommentGenerationFailedException에 대해서만 addFailed를 호출할 수 있는 점이 우려되었는데, 코드를 확인해보니 어떤 경우든 응답 만들기에 실패하면 위 에러가 발생하기 때문에 문제 없을 듯 합니다!
🔗 연관 이슈
📌 문제
LLM 생성은 파싱/검증 실패 시 최대 2회 재시도하는데,
generation_log에 한 시도의 토큰만 기록되어 재시도로 소모된 토큰이 누락됨 → 비용 집계·유저 일일 토큰 상한이 모두 과소 집계.output.usedTokens(2차만) 기록 → 1차 과금 토큰 소실lastError?.usedTokens(마지막만) 기록 → 이전 시도 토큰 소실🔧 변경
TokenUsageAccumulator도입 — 시도별 토큰(성공은 output, 실패는 예외에 실린 토큰; 호출 자체 실패면 null→0)을 합산.generateWithRetry/generateReplyWithRetry: 모든 시도의 토큰을 누적해 성공·최종실패·재시도불가 로그 모두 누적값으로 기록.CardService)는 재시도 없음(단일 호출) → 변경 없음.🧪 테스트
TokenUsageAccumulatorTest: 성공+실패 누적, null→0.스코프
generation_log(비용·일일한도 집계) 정확성만 수정. 앱 응답의GenerationResult.usedTokens(성공 시도 표시값)는 그대로.🌐 API · DB 영향
Summary by CodeRabbit
개선 사항
버그 수정