on main help sign in
fix: token calculation (#2004)
commit5ddf9d1068cc01d2cea20500cecd6fabf9370fb4 authorKujtim Hoxha <kujtimii.h@gmail.com> dateTue 27 Jan 2026, 14:11 (9 months ago) parentb944952
1 file changed+6−8
internal/agent/agent.go+6−8
internal/agent/agent.go+6−8
@@ -372,20 +372,18 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
372372 }
373373 currentAssistant.AddFinish(finishReason, "", "")
374374 sessionLock.Lock()
375− updatedSession, getSessionErr := a.sessions.Get(genCtx, call.SessionID)
375+ defer sessionLock.Unlock()
376+
377+ updatedSession, getSessionErr := a.sessions.Get(ctx, call.SessionID)
376378 if getSessionErr != nil {
377− sessionLock.Unlock()
378379 return getSessionErr
379380 }
380381 a.updateSessionUsage(largeModel, &updatedSession, stepResult.Usage, a.openrouterCost(stepResult.ProviderMetadata))
381− _, sessionErr := a.sessions.Save(genCtx, updatedSession)
382− if sessionErr == nil {
383− currentSession = updatedSession
384− }
385− sessionLock.Unlock()
382+ _, sessionErr := a.sessions.Save(ctx, updatedSession)
386383 if sessionErr != nil {
387384 return sessionErr
388385 }
386+ currentSession = updatedSession
389387 return a.messages.Update(genCtx, *currentAssistant)
390388 },
391389 StopWhen: []fantasy.StopCondition{
@@ -898,7 +896,7 @@ func (a *sessionAgent) updateSessionUsage(model Model, session *session.Session,
898896 }
899897
900898 session.CompletionTokens = usage.OutputTokens
901− session.PromptTokens = usage.InputTokens + usage.CacheCreationTokens
899+ session.PromptTokens = usage.InputTokens + usage.CacheReadTokens
902900}
903901
904902func (a *sessionAgent) Cancel(sessionID string) {