Accumulate per-call usage without copying event histories
This commit is contained in:
@@ -26,12 +26,20 @@ func BuildLLMExecution(snapshot llm.ObservationSnapshot) any {
|
||||
calls := make([]executionCall, 0, len(snapshot.Calls))
|
||||
events := make([]llm.ExecutionObservation, len(snapshot.Events))
|
||||
copy(events, snapshot.Events)
|
||||
eventsByCall := make(map[uint64][]llm.ExecutionObservation, len(snapshot.Calls))
|
||||
groups := make(map[uint64]*pk.ExecutionGroup, len(snapshot.Calls))
|
||||
for _, event := range events {
|
||||
eventsByCall[event.CallID] = append(eventsByCall[event.CallID], event)
|
||||
group := groups[event.CallID]
|
||||
if group == nil {
|
||||
group = &pk.ExecutionGroup{}
|
||||
groups[event.CallID] = group
|
||||
}
|
||||
group.Observe(event.PromptKit)
|
||||
}
|
||||
for _, call := range snapshot.Calls {
|
||||
group := llm.ObservationSnapshot{Calls: []llm.LogicalCall{call}, Events: eventsByCall[call.CallID]}
|
||||
group := groups[call.CallID]
|
||||
if group == nil {
|
||||
group = &pk.ExecutionGroup{}
|
||||
}
|
||||
var finished *time.Time
|
||||
if !call.Finished.IsZero() {
|
||||
value := call.Finished
|
||||
@@ -41,7 +49,7 @@ func BuildLLMExecution(snapshot llm.ObservationSnapshot) any {
|
||||
RequestedPromptID: call.RequestedPromptID, CallID: call.CallID, Context: call.Context,
|
||||
Prompt: call.Prompt, Started: call.Started, Finished: finished,
|
||||
ElapsedMS: call.WallDuration.Milliseconds(), Failure: call.Failure,
|
||||
PromptKit: llm.Summarize(group).PromptKit,
|
||||
PromptKit: group.Snapshot(),
|
||||
})
|
||||
}
|
||||
return struct {
|
||||
|
||||
Reference in New Issue
Block a user