Collect costs for dispatched model attempts
This commit is contained in:
@@ -58,6 +58,60 @@ func TestObservedRetryAndExhaustion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatchedAttemptCostsAreCollectedWithoutObserver(t *testing.T) {
|
||||
calls := 0
|
||||
coordinator := testCoordinator(t, func(*http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
return response(503, `{"error":{}}`), nil
|
||||
}
|
||||
return response(200, `{"usage":{"cost":0.02},"choices":[{"message":{"content":"ok"}}]}`), nil
|
||||
})
|
||||
var cost observation.CostAccumulator
|
||||
ctx := observation.WithCostAccumulator(context.Background(), &cost)
|
||||
req := testRequest(1)
|
||||
req.Target.Provider = "openrouter"
|
||||
if _, err := coordinator.Generate(ctx, req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := cost.Observed()
|
||||
if got.Value != .02 || !got.Known || got.Complete || got.Source != domain.CostSourceProvider {
|
||||
t.Fatalf("cost=%+v", got)
|
||||
}
|
||||
|
||||
configured := testCoordinator(t, func(*http.Request) (*http.Response, error) { return nil, errors.New("transport failed") })
|
||||
var zero observation.CostAccumulator
|
||||
req.Target.ZeroCost = true
|
||||
if _, err := configured.Generate(observation.WithCostAccumulator(context.Background(), &zero), req); err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if got := zero.Observed(); got != (domain.ObservedCost{Known: true, Complete: true, Source: domain.CostSourceConfiguredZero}) {
|
||||
t.Fatalf("fallback=%+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectedFailedResponseCostIsObservedOnce(t *testing.T) {
|
||||
configured := testCoordinator(t, func(*http.Request) (*http.Response, error) { t.Fatal("unexpected HTTP"); return nil, nil })
|
||||
charge := .25
|
||||
coordinator := NewInjected(configured.manager, fixedClient{response: &domain.GenerateResponse{CostUSD: &charge}, err: errors.New("failed")})
|
||||
var cost observation.CostAccumulator
|
||||
var events []domain.ExecutionEvent
|
||||
recorder, err := observation.New(func(event domain.ExecutionEvent) { events = append(events, event) })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder.SetCostAccumulator(&cost)
|
||||
ctx := observation.WithRecorder(observation.WithCostAccumulator(context.Background(), &cost), recorder)
|
||||
if _, err := coordinator.Generate(ctx, testRequest(0)); err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
recorder.Finish(observation.FinishData{})
|
||||
want := domain.ObservedCost{Value: .25, Known: true, Complete: true, Source: domain.CostSourceProvider}
|
||||
if len(events) != 2 || events[0].CostUSD != want || events[1].CostUSD != want || cost.Observed() != want {
|
||||
t.Fatalf("events=%+v cost=%+v", events, cost.Observed())
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservedInterruptedWaitAndCanceledCapacity(t *testing.T) {
|
||||
coordinator := testCoordinator(t, func(*http.Request) (*http.Response, error) { return response(503, `{"error":{}}`), nil })
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
Reference in New Issue
Block a user