@@ -372,20 +372,18 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
373 currentAssistant.AddFinish(finishReason, "", "")
373 currentAssistant.AddFinish(finishReason, "", "")
374 sessionLock.Lock()
374 sessionLock.Lock()
375+ defer sessionLock.Unlock()
375− updatedSession, getSessionErr := a.sessions.Get(genCtx, call.SessionID)
377+ updatedSession, getSessionErr := a.sessions.Get(ctx, call.SessionID)
376 if getSessionErr != nil {
378 if getSessionErr != nil {
377− sessionLock.Unlock()
378 return getSessionErr
379 return getSessionErr
380 a.updateSessionUsage(largeModel, &updatedSession, stepResult.Usage, a.openrouterCost(stepResult.ProviderMetadata))
381 a.updateSessionUsage(largeModel, &updatedSession, stepResult.Usage, a.openrouterCost(stepResult.ProviderMetadata))
381− _, sessionErr := a.sessions.Save(genCtx, updatedSession)
382+ _, sessionErr := a.sessions.Save(ctx, updatedSession)
382− if sessionErr == nil {
383− currentSession = updatedSession
385− sessionLock.Unlock()
386 if sessionErr != nil {
383 if sessionErr != nil {
387 return sessionErr
384 return sessionErr
386+ currentSession = updatedSession
389 return a.messages.Update(genCtx, *currentAssistant)
387 return a.messages.Update(genCtx, *currentAssistant)
391 StopWhen: []fantasy.StopCondition{
389 StopWhen: []fantasy.StopCondition{
@@ -898,7 +896,7 @@ func (a *sessionAgent) updateSessionUsage(model Model, session *session.Session,
900 session.CompletionTokens = usage.OutputTokens
898 session.CompletionTokens = usage.OutputTokens
901− session.PromptTokens = usage.InputTokens + usage.CacheCreationTokens
899+ session.PromptTokens = usage.InputTokens + usage.CacheReadTokens
904func (a *sessionAgent) Cancel(sessionID string) {
902func (a *sessionAgent) Cancel(sessionID string) {