Repository navigation
cost for prompt guardrails #4931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package schemas | ||
|
|
||
| // Clone returns an owned snapshot of cache debug data. | ||
| func (d *BifrostCacheDebug) Clone() *BifrostCacheDebug { | ||
| if d == nil { | ||
| return nil | ||
| } | ||
| clone := *d | ||
| clone.CacheID = cloneString(d.CacheID) | ||
| clone.HitType = cloneString(d.HitType) | ||
| clone.RequestedProvider = cloneString(d.RequestedProvider) | ||
| clone.RequestedModel = cloneString(d.RequestedModel) | ||
| clone.ProviderUsed = cloneString(d.ProviderUsed) | ||
| clone.ModelUsed = cloneString(d.ModelUsed) | ||
| clone.InputTokens = cloneInt(d.InputTokens) | ||
| clone.Threshold = cloneFloat64(d.Threshold) | ||
| clone.Similarity = cloneFloat64(d.Similarity) | ||
| clone.CacheHitLatency = cloneInt64(d.CacheHitLatency) | ||
| return &clone | ||
| } | ||
|
|
||
| // CacheDebugFromContext returns typed cache debug data stored on ctx. | ||
| func CacheDebugFromContext(ctx *BifrostContext) (*BifrostCacheDebug, bool) { | ||
| if ctx == nil { | ||
| return nil, false | ||
| } | ||
| debug, ok := ctx.Value(BifrostContextKeyCacheDebug).(*BifrostCacheDebug) | ||
| if !ok || debug == nil || debug.ProviderUsed == nil || debug.ModelUsed == nil || debug.InputTokens == nil { | ||
| return nil, false | ||
| } | ||
| return debug.Clone(), true | ||
| } | ||
|
|
||
| // SetCacheDebugOnContext stores semantic cache debug data on ctx. | ||
| func SetCacheDebugOnContext(ctx *BifrostContext, debug *BifrostCacheDebug) bool { | ||
| if ctx == nil || debug == nil || debug.ProviderUsed == nil || debug.ModelUsed == nil || debug.InputTokens == nil { | ||
| return false | ||
| } | ||
| ctx.SetValue(BifrostContextKeyCacheDebug, debug.Clone()) | ||
| return true | ||
| } | ||
|
|
||
| // cloneString returns an owned copy of value. | ||
| func cloneString(value *string) *string { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
| clone := *value | ||
| return &clone | ||
| } | ||
|
|
||
| // cloneInt returns an owned copy of value. | ||
| func cloneInt(value *int) *int { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
| clone := *value | ||
| return &clone | ||
| } | ||
|
|
||
| // cloneFloat64 returns an owned copy of value. | ||
| func cloneFloat64(value *float64) *float64 { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
| clone := *value | ||
| return &clone | ||
| } | ||
|
|
||
| // cloneInt64 returns an owned copy of value. | ||
| func cloneInt64(value *int64) *int64 { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
| clone := *value | ||
| return &clone | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package schemas | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestCacheDebugContextReturnsOwnedSnapshot verifies semantic cache metadata survives without aliases. | ||
| func TestCacheDebugContextReturnsOwnedSnapshot(t *testing.T) { | ||
| ctx := NewBifrostContext(nil, NoDeadline) | ||
| provider := "openai" | ||
| model := "text-embedding-3-small" | ||
| inputTokens := 12 | ||
| require.True(t, SetCacheDebugOnContext(ctx, &BifrostCacheDebug{ | ||
| ProviderUsed: &provider, | ||
| ModelUsed: &model, | ||
| InputTokens: &inputTokens, | ||
| })) | ||
|
|
||
| first, ok := CacheDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| *first.InputTokens = 999 | ||
|
|
||
| second, ok := CacheDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 12, *second.InputTokens) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package schemas | ||
|
|
||
| // BifrostGuardrailDebug carries request-scoped guardrail execution metadata. | ||
| type BifrostGuardrailDebug struct { | ||
| JudgeCalls []BifrostGuardrailJudgeCall `json:"judge_calls,omitempty"` | ||
| } | ||
|
|
||
| // BifrostGuardrailJudgeCall records one billable guardrail judge invocation. | ||
| type BifrostGuardrailJudgeCall struct { | ||
| Phase string `json:"phase,omitempty"` | ||
| RuleID *uint `json:"rule_id,omitempty"` | ||
| RuleName string `json:"rule_name,omitempty"` | ||
| GuardrailName string `json:"guardrail_name,omitempty"` | ||
| GuardrailProvider string `json:"guardrail_provider,omitempty"` | ||
| Action string `json:"action,omitempty"` | ||
| Reason string `json:"reason,omitempty"` | ||
| JudgeProvider ModelProvider `json:"judge_provider,omitempty"` | ||
| JudgeModel string `json:"judge_model,omitempty"` | ||
| JudgeRequestType RequestType `json:"judge_request_type,omitempty"` | ||
| PromptTokens int `json:"prompt_tokens,omitempty"` | ||
| PromptTokensDetails *ChatPromptTokensDetails `json:"prompt_tokens_details,omitempty"` | ||
| CompletionTokens int `json:"completion_tokens,omitempty"` | ||
| CompletionTokensDetails *ChatCompletionTokensDetails `json:"completion_tokens_details,omitempty"` | ||
| TotalTokens int `json:"total_tokens,omitempty"` | ||
| } | ||
|
|
||
| // Clone returns an owned snapshot of the guardrail debug data. | ||
| func (d *BifrostGuardrailDebug) Clone() *BifrostGuardrailDebug { | ||
| if d == nil || len(d.JudgeCalls) == 0 { | ||
| return nil | ||
| } | ||
| clone := &BifrostGuardrailDebug{ | ||
| JudgeCalls: make([]BifrostGuardrailJudgeCall, len(d.JudgeCalls)), | ||
| } | ||
| for index, call := range d.JudgeCalls { | ||
| clone.JudgeCalls[index] = call | ||
| clone.JudgeCalls[index].RuleID = cloneUint(call.RuleID) | ||
| clone.JudgeCalls[index].PromptTokensDetails = cloneChatPromptTokensDetails(call.PromptTokensDetails) | ||
| clone.JudgeCalls[index].CompletionTokensDetails = cloneChatCompletionTokensDetails(call.CompletionTokensDetails) | ||
| } | ||
| return clone | ||
| } | ||
|
|
||
| // cloneChatPromptTokensDetails returns an owned copy of nested prompt token details. | ||
| func cloneChatPromptTokensDetails(details *ChatPromptTokensDetails) *ChatPromptTokensDetails { | ||
| if details == nil { | ||
| return nil | ||
| } | ||
| clone := *details | ||
| if details.CachedWriteTokenDetails != nil { | ||
| cachedWriteClone := *details.CachedWriteTokenDetails | ||
| clone.CachedWriteTokenDetails = &cachedWriteClone | ||
| } | ||
| return &clone | ||
| } | ||
|
|
||
| // cloneChatCompletionTokensDetails returns an owned copy of completion token details. | ||
| func cloneChatCompletionTokensDetails(details *ChatCompletionTokensDetails) *ChatCompletionTokensDetails { | ||
| if details == nil { | ||
| return nil | ||
| } | ||
| clone := *details | ||
| clone.CitationTokens = cloneInt(details.CitationTokens) | ||
| clone.NumSearchQueries = cloneInt(details.NumSearchQueries) | ||
| clone.ImageTokens = cloneInt(details.ImageTokens) | ||
| return &clone | ||
| } | ||
|
|
||
| // cloneUint returns an owned copy of value. | ||
| func cloneUint(value *uint) *uint { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
| clone := *value | ||
| return &clone | ||
| } | ||
|
|
||
| // GuardrailDebugFromContext returns typed guardrail debug data stored on ctx. | ||
| func GuardrailDebugFromContext(ctx *BifrostContext) (*BifrostGuardrailDebug, bool) { | ||
| if ctx == nil { | ||
| return nil, false | ||
| } | ||
| debug, ok := ctx.Value(BifrostContextKeyGuardrailDebug).(*BifrostGuardrailDebug) | ||
| if !ok || debug == nil || len(debug.JudgeCalls) == 0 { | ||
| return nil, false | ||
| } | ||
| return debug.Clone(), true | ||
| } | ||
|
|
||
| // SetGuardrailDebugOnContext stores non-empty guardrail debug data on ctx. | ||
| func SetGuardrailDebugOnContext(ctx *BifrostContext, debug *BifrostGuardrailDebug) bool { | ||
| if ctx == nil || debug == nil || len(debug.JudgeCalls) == 0 { | ||
| return false | ||
| } | ||
| ctx.SetValue(BifrostContextKeyGuardrailDebug, debug.Clone()) | ||
| return true | ||
| } | ||
|
|
||
| // AppendGuardrailJudgeCallOnContext appends one guardrail judge call to ctx. | ||
| func AppendGuardrailJudgeCallOnContext(ctx *BifrostContext, call BifrostGuardrailJudgeCall) bool { | ||
| if ctx == nil || call.TotalTokens == 0 && call.PromptTokens == 0 && call.CompletionTokens == 0 { | ||
| return false | ||
| } | ||
| current, _ := GuardrailDebugFromContext(ctx) | ||
| if current == nil { | ||
| current = &BifrostGuardrailDebug{} | ||
| } | ||
| current.JudgeCalls = append(current.JudgeCalls, call) | ||
| return SetGuardrailDebugOnContext(ctx, current) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package schemas | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestGuardrailDebugContextRoundTrip verifies typed guardrail debug context storage. | ||
| func TestGuardrailDebugContextRoundTrip(t *testing.T) { | ||
| ctx := NewBifrostContext(nil, NoDeadline) | ||
| call := BifrostGuardrailJudgeCall{ | ||
| Phase: "input", | ||
| RuleName: "pii", | ||
| JudgeProvider: OpenAI, | ||
| JudgeModel: "gpt-4o-mini", | ||
| PromptTokens: 12, | ||
| TotalTokens: 12, | ||
| } | ||
|
|
||
| require.True(t, AppendGuardrailJudgeCallOnContext(ctx, call)) | ||
| debug, ok := GuardrailDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| require.Len(t, debug.JudgeCalls, 1) | ||
| assert.Equal(t, call, debug.JudgeCalls[0]) | ||
| } | ||
|
|
||
| // TestGuardrailDebugContextReturnsOwnedSnapshot verifies callers cannot mutate context state. | ||
| func TestGuardrailDebugContextReturnsOwnedSnapshot(t *testing.T) { | ||
| ctx := NewBifrostContext(nil, NoDeadline) | ||
| require.True(t, AppendGuardrailJudgeCallOnContext(ctx, BifrostGuardrailJudgeCall{ | ||
| JudgeProvider: OpenAI, | ||
| JudgeModel: "gpt-4o-mini", | ||
| TotalTokens: 10, | ||
| })) | ||
|
|
||
| first, ok := GuardrailDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| first.JudgeCalls[0].TotalTokens = 999 | ||
|
|
||
| second, ok := GuardrailDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 10, second.JudgeCalls[0].TotalTokens) | ||
| } | ||
|
|
||
| // TestGuardrailDebugContextClonesUsageDetails verifies nested pricing details cannot alias context state. | ||
| func TestGuardrailDebugContextClonesUsageDetails(t *testing.T) { | ||
| ctx := NewBifrostContext(nil, NoDeadline) | ||
| citationTokens := 3 | ||
| require.True(t, AppendGuardrailJudgeCallOnContext(ctx, BifrostGuardrailJudgeCall{ | ||
| JudgeProvider: OpenAI, | ||
| JudgeModel: "gpt-4o-mini", | ||
| PromptTokens: 10, | ||
| PromptTokensDetails: &ChatPromptTokensDetails{ | ||
| CachedWriteTokenDetails: &ChatCachedWriteTokenDetails{CachedWriteTokens5m: 4}, | ||
| }, | ||
| CompletionTokens: 5, | ||
| CompletionTokensDetails: &ChatCompletionTokensDetails{ | ||
| CitationTokens: &citationTokens, | ||
| }, | ||
| TotalTokens: 15, | ||
| })) | ||
|
|
||
| first, ok := GuardrailDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| first.JudgeCalls[0].PromptTokensDetails.CachedWriteTokenDetails.CachedWriteTokens5m = 999 | ||
| *first.JudgeCalls[0].CompletionTokensDetails.CitationTokens = 999 | ||
|
|
||
| second, ok := GuardrailDebugFromContext(ctx) | ||
| require.True(t, ok) | ||
| assert.Equal(t, 4, second.JudgeCalls[0].PromptTokensDetails.CachedWriteTokenDetails.CachedWriteTokens5m) | ||
| assert.Equal(t, 3, *second.JudgeCalls[0].CompletionTokensDetails.CitationTokens) | ||
| } | ||
|
|
||
| // TestAppendGuardrailJudgeCallRejectsEmptyUsage verifies non-billable calls are omitted. | ||
| func TestAppendGuardrailJudgeCallRejectsEmptyUsage(t *testing.T) { | ||
| ctx := NewBifrostContext(nil, NoDeadline) | ||
| assert.False(t, AppendGuardrailJudgeCallOnContext(ctx, BifrostGuardrailJudgeCall{})) | ||
| _, ok := GuardrailDebugFromContext(ctx) | ||
| assert.False(t, ok) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.