diff --git a/aibridge/bridge.go b/aibridge/bridge.go index a46fc39b2f0..e238fb57e12 100644 --- a/aibridge/bridge.go +++ b/aibridge/bridge.go @@ -222,6 +222,7 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC interceptor.Setup(logger, asyncRecorder, mcpProxy) if err := rec.RecordInterception(ctx, &recorder.InterceptionRecord{ + StartedAt: time.Now().UTC(), ID: interceptor.ID().String(), InitiatorID: actor.ID, Metadata: actor.Metadata, @@ -284,6 +285,7 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC } _ = asyncRecorder.RecordInterceptionEnded(ctx, &recorder.InterceptionRecordEnded{ + EndedAt: time.Now().UTC(), ID: interceptor.ID().String(), CredentialHint: cred.Hint(), ErrorType: errType, diff --git a/aibridge/intercept/chatcompletions/base.go b/aibridge/intercept/chatcompletions/base.go index fe72c3f2ab5..3d088b2b849 100644 --- a/aibridge/intercept/chatcompletions/base.go +++ b/aibridge/intercept/chatcompletions/base.go @@ -9,6 +9,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/google/uuid" "github.com/openai/openai-go/v3" @@ -277,6 +278,7 @@ func (i *interceptionBase) recordTokenUsage(ctx context.Context, msgID string, u } _ = i.recorder.RecordTokenUsage(ctx, &recorder.TokenUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: msgID, Input: calculateActualInputTokenUsage(usage), diff --git a/aibridge/intercept/chatcompletions/blocking.go b/aibridge/intercept/chatcompletions/blocking.go index 23ed5acae61..ea39c060ecf 100644 --- a/aibridge/intercept/chatcompletions/blocking.go +++ b/aibridge/intercept/chatcompletions/blocking.go @@ -135,6 +135,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req if prompt != nil { _ = i.recorder.RecordPromptUsage(ctx, &recorder.PromptUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: completion.ID, Prompt: *prompt, @@ -155,6 +156,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req pendingToolCalls = append(pendingToolCalls, toolCall) } else { _ = i.recorder.RecordToolUsage(ctx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: completion.ID, ToolCallID: toolCall.ID, @@ -193,6 +195,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req args := i.unmarshalArgs(tc.Function.Arguments) res, err := tool.Call(ctx, args, i.tracer) _ = i.recorder.RecordToolUsage(ctx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: completion.ID, ToolCallID: tc.ID, diff --git a/aibridge/intercept/chatcompletions/streaming.go b/aibridge/intercept/chatcompletions/streaming.go index 5a593f8e25f..ddcf4318360 100644 --- a/aibridge/intercept/chatcompletions/streaming.go +++ b/aibridge/intercept/chatcompletions/streaming.go @@ -265,6 +265,7 @@ func (i *StreamingInterception) ProcessRequest(w http.ResponseWriter, r *http.Re // Builtin tools are not intercepted. if i.getInjectedToolByName(toolCall.Name) == nil { _ = i.recorder.RecordToolUsage(streamCtx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: processor.getMsgID(), ToolCallID: toolCall.ID, @@ -287,6 +288,7 @@ func (i *StreamingInterception) ProcessRequest(w http.ResponseWriter, r *http.Re if prompt != nil { _ = i.recorder.RecordPromptUsage(streamCtx, &recorder.PromptUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: processor.getMsgID(), Prompt: *prompt, @@ -375,6 +377,7 @@ func (i *StreamingInterception) ProcessRequest(w http.ResponseWriter, r *http.Re args := i.unmarshalArgs(toolCall.Arguments) toolRes, toolErr := tool.Call(streamCtx, args, i.tracer) _ = i.recorder.RecordToolUsage(streamCtx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: processor.getMsgID(), ToolCallID: id, diff --git a/aibridge/intercept/messages/base.go b/aibridge/intercept/messages/base.go index cee00502ee2..e71e5317384 100644 --- a/aibridge/intercept/messages/base.go +++ b/aibridge/intercept/messages/base.go @@ -182,6 +182,7 @@ func (i *interceptionBase) recordTokenUsage(ctx context.Context, msgID string, u } usageRecord := recorder.TokenUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: msgID, Input: usage.InputTokens, @@ -318,8 +319,9 @@ func (*interceptionBase) extractModelThoughts(msg *anthropic.Message) []*recorde continue } thoughtRecords = append(thoughtRecords, &recorder.ModelThoughtRecord{ - Content: variant.Thinking, - Metadata: recorder.Metadata{"source": recorder.ThoughtSourceThinking}, + CreatedAt: time.Now().UTC(), + Content: variant.Thinking, + Metadata: recorder.Metadata{"source": recorder.ThoughtSourceThinking}, }) } return thoughtRecords diff --git a/aibridge/intercept/messages/base_internal_test.go b/aibridge/intercept/messages/base_internal_test.go index e390d373ecf..994e7715133 100644 --- a/aibridge/intercept/messages/base_internal_test.go +++ b/aibridge/intercept/messages/base_internal_test.go @@ -1670,6 +1670,11 @@ func TestRecordTokenUsage(t *testing.T) { usages := rec.RecordedTokenUsages() require.Len(t, usages, 1) + + // The record carries the time it was created; the rest of the + // record is compared verbatim. + require.False(t, usages[0].CreatedAt.IsZero(), "token usage was not stamped") + usages[0].CreatedAt = time.Time{} require.Equal(t, tc.expected, usages[0]) }) } diff --git a/aibridge/intercept/messages/blocking.go b/aibridge/intercept/messages/blocking.go index 4b7088b211b..96127760f2f 100644 --- a/aibridge/intercept/messages/blocking.go +++ b/aibridge/intercept/messages/blocking.go @@ -142,6 +142,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req if prompt != nil { _ = i.recorder.RecordPromptUsage(ctx, &recorder.PromptUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: resp.ID, Prompt: *prompt, @@ -156,6 +157,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req // Capture any thinking blocks that were returned. for _, t := range i.extractModelThoughts(resp) { _ = i.recorder.RecordModelThought(ctx, &recorder.ModelThoughtRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), Content: t.Content, Metadata: t.Metadata, @@ -177,6 +179,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req // If tool is not injected, track it since the client will be handling it. _ = i.recorder.RecordToolUsage(ctx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: resp.ID, ToolCallID: toolUse.ID, @@ -213,6 +216,7 @@ func (i *BlockingInterception) ProcessRequest(w http.ResponseWriter, r *http.Req res, err := tool.Call(ctx, tc.Input, i.tracer) _ = i.recorder.RecordToolUsage(ctx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: resp.ID, ToolCallID: tc.ID, diff --git a/aibridge/intercept/messages/streaming.go b/aibridge/intercept/messages/streaming.go index 94d496130aa..dafc673f0b8 100644 --- a/aibridge/intercept/messages/streaming.go +++ b/aibridge/intercept/messages/streaming.go @@ -315,6 +315,7 @@ newStream: // Capture any thinking blocks that were returned. for _, t := range i.extractModelThoughts(&message) { _ = i.recorder.RecordModelThought(ctx, &recorder.ModelThoughtRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), Content: t.Content, Metadata: t.Metadata, @@ -366,6 +367,7 @@ newStream: res, err := tool.Call(streamCtx, input, i.tracer) _ = i.recorder.RecordToolUsage(streamCtx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: message.ID, ToolCallID: id, @@ -490,6 +492,7 @@ newStream: } _ = i.recorder.RecordToolUsage(streamCtx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: message.ID, ToolCallID: variant.ID, @@ -521,6 +524,7 @@ newStream: if promptFound { _ = i.recorder.RecordPromptUsage(ctx, &recorder.PromptUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: message.ID, Prompt: prompt, diff --git a/aibridge/intercept/responses/base.go b/aibridge/intercept/responses/base.go index 4bbedf3a1d4..eca80507c4f 100644 --- a/aibridge/intercept/responses/base.go +++ b/aibridge/intercept/responses/base.go @@ -284,6 +284,7 @@ func (i *responsesInterceptionBase) recordUserPrompt(ctx context.Context, respon } promptUsage := &recorder.PromptUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: responseID, Prompt: prompt, @@ -296,6 +297,7 @@ func (i *responsesInterceptionBase) recordUserPrompt(ctx context.Context, respon func (i *responsesInterceptionBase) recordModelThoughts(ctx context.Context, response *responses.Response) { for _, t := range i.extractModelThoughts(response) { _ = i.recorder.RecordModelThought(ctx, &recorder.ModelThoughtRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), Content: t.Content, Metadata: t.Metadata, @@ -346,6 +348,7 @@ func (i *responsesInterceptionBase) recordNonInjectedToolUsage(ctx context.Conte } if err := i.recorder.RecordToolUsage(ctx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: response.ID, // ItemID is always present; ToolCallID (call_id) is empty for @@ -395,6 +398,7 @@ func (i *responsesInterceptionBase) recordTokenUsage(ctx context.Context, respon } if err := i.recorder.RecordTokenUsage(ctx, &recorder.TokenUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: response.ID, Input: inputNonCacheTokens, @@ -429,8 +433,9 @@ func (*responsesInterceptionBase) extractModelThoughts(response *responses.Respo continue } thoughts = append(thoughts, &recorder.ModelThoughtRecord{ - Content: summary.Text, - Metadata: recorder.Metadata{"source": recorder.ThoughtSourceReasoningSummary}, + CreatedAt: time.Now().UTC(), + Content: summary.Text, + Metadata: recorder.Metadata{"source": recorder.ThoughtSourceReasoningSummary}, }) } @@ -453,8 +458,9 @@ func (*responsesInterceptionBase) extractModelThoughts(response *responses.Respo continue } thoughts = append(thoughts, &recorder.ModelThoughtRecord{ - Content: part.Text, - Metadata: recorder.Metadata{"source": recorder.ThoughtSourceCommentary}, + CreatedAt: time.Now().UTC(), + Content: part.Text, + Metadata: recorder.Metadata{"source": recorder.ThoughtSourceCommentary}, }) } } diff --git a/aibridge/intercept/responses/injected_tools.go b/aibridge/intercept/responses/injected_tools.go index 65daa232098..875a87cf8f8 100644 --- a/aibridge/intercept/responses/injected_tools.go +++ b/aibridge/intercept/responses/injected_tools.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "strings" + "time" "github.com/openai/openai-go/v3" "github.com/openai/openai-go/v3/responses" @@ -179,6 +180,7 @@ func (i *responsesInterceptionBase) invokeInjectedTool(ctx context.Context, resp args := i.parseFunctionCallJSONArgs(ctx, fc.Arguments) res, err := tool.Call(ctx, args, i.tracer) _ = i.recorder.RecordToolUsage(ctx, &recorder.ToolUsageRecord{ + CreatedAt: time.Now().UTC(), InterceptionID: i.ID().String(), MsgID: responseID, ToolCallID: fc.CallID, diff --git a/aibridge/recorder/wrapped_recorder.go b/aibridge/recorder/wrapped_recorder.go index dcad19f016b..12118fd786f 100644 --- a/aibridge/recorder/wrapped_recorder.go +++ b/aibridge/recorder/wrapped_recorder.go @@ -2,7 +2,6 @@ package recorder import ( "context" - "time" "golang.org/x/xerrors" ) @@ -10,7 +9,6 @@ import ( var _ Recorder = &WrappedRecorder{} // WrappedRecorder is a convenience struct which implements Recorder and resolves a client before calling each method. -// It also sets the start/creation time of each record. type WrappedRecorder struct { clientFn func(context.Context) (Recorder, error) } @@ -21,7 +19,6 @@ func (r *WrappedRecorder) RecordInterception(ctx context.Context, req *Intercept return xerrors.Errorf("acquire client: %w", err) } - req.StartedAt = time.Now() return client.RecordInterception(ctx, req) } @@ -31,7 +28,6 @@ func (r *WrappedRecorder) RecordInterceptionEnded(ctx context.Context, req *Inte return xerrors.Errorf("acquire client: %w", err) } - req.EndedAt = time.Now().UTC() return client.RecordInterceptionEnded(ctx, req) } @@ -41,7 +37,6 @@ func (r *WrappedRecorder) RecordPromptUsage(ctx context.Context, req *PromptUsag return xerrors.Errorf("acquire client: %w", err) } - req.CreatedAt = time.Now() return client.RecordPromptUsage(ctx, req) } @@ -51,7 +46,6 @@ func (r *WrappedRecorder) RecordTokenUsage(ctx context.Context, req *TokenUsageR return xerrors.Errorf("acquire client: %w", err) } - req.CreatedAt = time.Now() return client.RecordTokenUsage(ctx, req) } @@ -61,7 +55,6 @@ func (r *WrappedRecorder) RecordToolUsage(ctx context.Context, req *ToolUsageRec return xerrors.Errorf("acquire client: %w", err) } - req.CreatedAt = time.Now() return client.RecordToolUsage(ctx, req) } @@ -71,7 +64,6 @@ func (r *WrappedRecorder) RecordModelThought(ctx context.Context, req *ModelThou return xerrors.Errorf("acquire client: %w", err) } - req.CreatedAt = time.Now() return client.RecordModelThought(ctx, req) }