Callback to handle rate limiting based on the number of requests or the number of tokens in the input.

It uses Upstash Ratelimit to track the rate limit which utilizes Upstash Redis to track the state.

Should not be passed to the chain when initializing the chain. This is because the handler has a state which should be fresh every time invoke is called. Instead, initialize and pass a handler every time you invoke.

Hierarchy

  • BaseCallbackHandler
    • UpstashRatelimitHandler

Constructors

Properties

awaitHandlers: boolean
identifier: string
ignoreAgent: boolean
ignoreChain: boolean
ignoreCustomEvent: boolean
ignoreLLM: boolean
ignoreRetriever: boolean
includeOutputTokens: boolean
llmOutputPromptTokenField: string
llmOutputTokenUsageField: string
llmOutputTotalTokenField: string
name: string = "UpstashRatelimit"
raiseError: boolean = true
requestRatelimit?: RegionRatelimit
tokenRatelimit?: RegionRatelimit

Methods

  • Returns BaseCallbackHandler

  • Called when an agent is about to execute an action, with the action and the run ID.

    Parameters

    • action: AgentAction
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns void | Promise<void>

  • Called when an agent finishes execution, before it exits. with the final output and the run ID.

    Parameters

    • action: AgentFinish
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns void | Promise<void>

  • Called at the end of a Chain run, with the outputs and the run ID.

    Parameters

    • outputs: ChainValues
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]
    • Optionalkwargs: {
          inputs?: Record<string, unknown>;
      }
      • Optionalinputs?: Record<string, unknown>

    Returns any

  • Called if a Chain run encounters an error

    Parameters

    • err: any
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]
    • Optionalkwargs: {
          inputs?: Record<string, unknown>;
      }
      • Optionalinputs?: Record<string, unknown>

    Returns any

  • Run when the chain starts running.

    This method is called multiple times during a chain execution. To ensure it only runs once, it checks and updates a _checked state.

    Parameters

    • _chain: Serialized

      Serialized chain

    • _inputs: ChainValues

      Chain input values

    Returns Promise<void>

    UpstashRatelimitError - If the request rate limit is reached

  • Called at the start of a Chat Model run, with the prompt(s) and the run ID.

    Parameters

    • llm: Serialized
    • messages: BaseMessage[][]
    • runId: string
    • OptionalparentRunId: string
    • OptionalextraParams: Record<string, unknown>
    • Optionaltags: string[]
    • Optionalmetadata: Record<string, unknown>
    • OptionalrunName: string

    Returns any

  • Parameters

    • eventName: string
    • data: any
    • runId: string
    • Optionaltags: string[]
    • Optionalmetadata: Record<string, any>

    Returns any

  • Run when the LLM ends running.

    If the includeOutputTokens is set to true, the number of tokens in the LLM completion are counted for rate limiting.

    Parameters

    • output: LLMResult

      LLM result output

    • _runId: string
    • Optional_parentRunId: string
    • Optional_tags: string[]

    Returns Promise<void>

    Error - If the LLM response does not include required token usage information

  • Called if an LLM/ChatModel run encounters an error

    Parameters

    • err: any
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns any

  • Called when an LLM/ChatModel in streaming mode produces a new token

    Parameters

    • token: string
    • idx: NewTokenIndices

      idx.prompt is the index of the prompt that produced the token (if there are multiple prompts) idx.completion is the index of the completion that produced the token (if multiple completions per prompt are requested)

    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]
    • Optionalfields: HandleLLMNewTokenCallbackFields

    Returns any

  • Run when the LLM starts running.

    Parameters

    • _llm: Serialized

      Serialized LLM

    • _prompts: string[]

      Prompts passed to the LLM

    • _runId: string
    • Optional_parentRunId: string
    • Optional_extraParams: Record<string, unknown>
    • Optional_tags: string[]
    • Optional_metadata: Record<string, unknown>
    • Optional_name: string

    Returns Promise<void>

    UpstashRatelimitError - If the token rate limit is reached

  • Parameters

    • documents: DocumentInterface<Record<string, any>>[]
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns any

  • Parameters

    • err: any
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns any

  • Parameters

    • retriever: Serialized
    • query: string
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]
    • Optionalmetadata: Record<string, unknown>
    • Optionalname: string

    Returns any

  • Parameters

    • text: string
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns void | Promise<void>

  • Called at the end of a Tool run, with the tool output and the run ID.

    Parameters

    • output: any
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns any

  • Called if a Tool run encounters an error

    Parameters

    • err: any
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]

    Returns any

  • Called at the start of a Tool run, with the tool name and input and the run ID.

    Parameters

    • tool: Serialized
    • input: string
    • runId: string
    • OptionalparentRunId: string
    • Optionaltags: string[]
    • Optionalmetadata: Record<string, unknown>
    • OptionalrunName: string

    Returns any

  • Returns Serialized

  • Parameters

    • methods: BaseCallbackHandlerMethodsClass

    Returns {
        awaitHandlers: boolean;
        ignoreAgent: boolean;
        ignoreChain: boolean;
        ignoreCustomEvent: boolean;
        ignoreLLM: boolean;
        ignoreRetriever: boolean;
        name: string;
        raiseError: boolean;
        copy(): BaseCallbackHandler;
        handleAgentAction?(action: AgentAction, runId: string, parentRunId?: string, tags?: string[]): void | Promise<void>;
        handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): void | Promise<void>;
        handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
            inputs?: Record<string, unknown>;
        }): any;
        handleChainError?(err: any, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
            inputs?: Record<string, unknown>;
        }): any;
        handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): any;
        handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, runName?: string): any;
        handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[], metadata?: Record<string, any>): any;
        handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string, tags?: string[]): any;
        handleLLMError?(err: any, runId: string, parentRunId?: string, tags?: string[]): any;
        handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): any;
        handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, runName?: string): any;
        handleRetrieverEnd?(documents: DocumentInterface<Record<string, any>>[], runId: string, parentRunId?: string, tags?: string[]): any;
        handleRetrieverError?(err: any, runId: string, parentRunId?: string, tags?: string[]): any;
        handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): any;
        handleText?(text: string, runId: string, parentRunId?: string, tags?: string[]): void | Promise<void>;
        handleToolEnd?(output: any, runId: string, parentRunId?: string, tags?: string[]): any;
        handleToolError?(err: any, runId: string, parentRunId?: string, tags?: string[]): any;
        handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runName?: string): any;
        toJSON(): Serialized;
        toJSONNotImplemented(): SerializedNotImplemented;
    }

    • awaitHandlers: boolean
    • ignoreAgent: boolean
    • ignoreChain: boolean
    • ignoreCustomEvent: boolean
    • ignoreLLM: boolean
    • ignoreRetriever: boolean
    • name: string
    • raiseError: boolean
    • copy:function
      • Returns BaseCallbackHandler

    • handleAgentAction?:function
      • Called when an agent is about to execute an action, with the action and the run ID.

        Parameters

        • action: AgentAction
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns void | Promise<void>

    • handleAgentEnd?:function
      • Called when an agent finishes execution, before it exits. with the final output and the run ID.

        Parameters

        • action: AgentFinish
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns void | Promise<void>

    • handleChainEnd?:function
      • Called at the end of a Chain run, with the outputs and the run ID.

        Parameters

        • outputs: ChainValues
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]
        • Optionalkwargs: {
              inputs?: Record<string, unknown>;
          }
          • Optionalinputs?: Record<string, unknown>

        Returns any

    • handleChainError?:function
      • Called if a Chain run encounters an error

        Parameters

        • err: any
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]
        • Optionalkwargs: {
              inputs?: Record<string, unknown>;
          }
          • Optionalinputs?: Record<string, unknown>

        Returns any

    • handleChainStart?:function
      • Called at the start of a Chain run, with the chain name and inputs and the run ID.

        Parameters

        • chain: Serialized
        • inputs: ChainValues
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]
        • Optionalmetadata: Record<string, unknown>
        • OptionalrunType: string
        • OptionalrunName: string

        Returns any

    • handleChatModelStart?:function
      • Called at the start of a Chat Model run, with the prompt(s) and the run ID.

        Parameters

        • llm: Serialized
        • messages: BaseMessage[][]
        • runId: string
        • OptionalparentRunId: string
        • OptionalextraParams: Record<string, unknown>
        • Optionaltags: string[]
        • Optionalmetadata: Record<string, unknown>
        • OptionalrunName: string

        Returns any

    • handleCustomEvent?:function
      • Parameters

        • eventName: string
        • data: any
        • runId: string
        • Optionaltags: string[]
        • Optionalmetadata: Record<string, any>

        Returns any

    • handleLLMEnd?:function
      • Called at the end of an LLM/ChatModel run, with the output and the run ID.

        Parameters

        • output: LLMResult
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns any

    • handleLLMError?:function
      • Called if an LLM/ChatModel run encounters an error

        Parameters

        • err: any
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns any

    • handleLLMNewToken?:function
      • Called when an LLM/ChatModel in streaming mode produces a new token

        Parameters

        • token: string
        • idx: NewTokenIndices
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]
        • Optionalfields: HandleLLMNewTokenCallbackFields

        Returns any

    • handleLLMStart?:function
      • Called at the start of an LLM or Chat Model run, with the prompt(s) and the run ID.

        Parameters

        • llm: Serialized
        • prompts: string[]
        • runId: string
        • OptionalparentRunId: string
        • OptionalextraParams: Record<string, unknown>
        • Optionaltags: string[]
        • Optionalmetadata: Record<string, unknown>
        • OptionalrunName: string

        Returns any

    • handleRetrieverEnd?:function
      • Parameters

        • documents: DocumentInterface<Record<string, any>>[]
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns any

    • handleRetrieverError?:function
      • Parameters

        • err: any
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns any

    • handleRetrieverStart?:function
      • Parameters

        • retriever: Serialized
        • query: string
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]
        • Optionalmetadata: Record<string, unknown>
        • Optionalname: string

        Returns any

    • handleText?:function
      • Parameters

        • text: string
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns void | Promise<void>

    • handleToolEnd?:function
      • Called at the end of a Tool run, with the tool output and the run ID.

        Parameters

        • output: any
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns any

    • handleToolError?:function
      • Called if a Tool run encounters an error

        Parameters

        • err: any
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]

        Returns any

    • handleToolStart?:function
      • Called at the start of a Tool run, with the tool name and input and the run ID.

        Parameters

        • tool: Serialized
        • input: string
        • runId: string
        • OptionalparentRunId: string
        • Optionaltags: string[]
        • Optionalmetadata: Record<string, unknown>
        • OptionalrunName: string

        Returns any

    • toJSON:function
    • toJSONNotImplemented:function