MCPcopy Index your code
hub / github.com/coder/coder / checkUsageLimit

Method checkUsageLimit

coderd/x/chatd/chatd.go:1907–1930  ·  view source on GitHub ↗
(ctx context.Context, store database.Store, ownerID uuid.UUID, organizationID uuid.NullUUID)

Source from the content-addressed store, hash-verified

1905}
1906
1907func (p *Server) checkUsageLimit(ctx context.Context, store database.Store, ownerID uuid.UUID, organizationID uuid.NullUUID) error {
1908 status, err := ResolveUsageLimitStatus(ctx, store, ownerID, organizationID, time.Now())
1909 if err != nil {
1910 // Fail open: never block chat due to a limit-resolution failure.
1911 p.logger.Warn(ctx, "usage limit check failed, allowing message",
1912 slog.F("owner_id", ownerID),
1913 slog.Error(err),
1914 )
1915 return nil
1916 }
1917 if status == nil {
1918 return nil
1919 }
1920 // Block when current spend reaches or exceeds limit (>= ensures
1921 // the user cannot start new conversations once the limit is hit).
1922 if status.SpendLimitMicros != nil && status.CurrentSpend >= *status.SpendLimitMicros {
1923 return &UsageLimitExceededError{
1924 LimitMicros: *status.SpendLimitMicros,
1925 ConsumedMicros: status.CurrentSpend,
1926 PeriodEnd: status.PeriodEnd,
1927 }
1928 }
1929 return nil
1930}
1931
1932func chatdModelConfigLookupContext(ctx context.Context) context.Context {
1933 //nolint:gocritic // Chat message admission needs daemon-scoped

Callers 5

CreateChatMethod · 0.95
SendMessageMethod · 0.95
EditMessageMethod · 0.95

Calls 2

ResolveUsageLimitStatusFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected