Rate Limiting in Workflows

Rate limiting is a crucial aspect of workflow management that helps control resource consumption and prevent any single workflow or activity from monopolizing shared resources.

How Rate Limiting Works

How Rate Limiting Works

Rate limits are always shared across all workers and workflows in the same deployment. The key parameter controls how activities share these limits:

Case 1: Rate Limit Per Activity (No Key Provided)

Case 1: Rate Limit Per Activity (No Key Provided)

When no key is provided, the rate limit applies to the activity itself and is shared across all workers and workflows that use it. The SDK uses the activity's function name as the key.

Use this when: The activity is the only one that calls a given upstream API.

Case 2: Rate Limit Across Activities (Key Provided)

Case 2: Rate Limit Across Activities (Key Provided)

When a key is provided, multiple different activities that use the same key share a single rate limit pool across all workers and workflows.

Use this when: Several activities call the same upstream API (for example, read, write, and delete operations against the same external service). See Choosing rate limit keys.

Example: Shared Rate-Limited API Client (No Key)

Example: Shared Rate-Limited API Client (No Key)

import mistralai.workflows as workflows
from mistralai.workflows.client import get_mistral_client
from mistralai.workflows import Depends
from pydantic import BaseModel

class CompletionParams(BaseModel):
    model: str
    messages: list

@workflows.activity(rate_limit=workflows.RateLimit(time_window_in_sec=1, max_execution=100))
async def generate_chat_response(
    params: CompletionParams,
    client = Depends(get_mistral_client)
) -> dict:
    """Generates a chat response using a shared client"""
    return await client.chat.complete_async(model=params.model, messages=params.messages)
Tip

get_mistral_client is the Workflows SDK's built-in factory for creating a properly configured Mistral client inside activities. It handles authentication, telemetry, and observability metadata automatically. See Mistral client for details.

Behavior: All workflows calling generate_chat_response share the same 100 executions/sec limit. If Workflow A makes 60 calls and Workflow B makes 50 calls in the same second, they compete for the same pool.

With a key: Add key="mistral_api" to share this limit across multiple activities (e.g., generate_chat_response, generate_embeddings, moderate_content).

Rate limit keys and poll limits

Rate limit keys and poll limits

Each distinct rate limit key adds an internal worker to every worker process that registers it. Too many keys in one process stall its workflows. Keep the number of keys low.

How keys add internal workers

How keys add internal workers

A single worker process runs:

  • 1 main worker, which runs your workflows and activities that have no rate limit
  • 1 internal worker per distinct rate limit key
  • 1 sticky-activity worker, only if an activity sets sticky_to_worker=True

An activity with a rate limit and no key uses its function name as its key, so it gets its own internal worker.

Activities that share a key but set different time_window_in_sec or max_execution values get one internal worker per distinct rate, not one per key. These workers poll the same task queue, which makes activities fail. See Choosing rate limit keys.

An activity that sets both sticky_to_worker=True and a rate limit runs on the sticky-activity worker. It adds no internal worker, and its rate limit is not enforced.

The SDK registers every activity in every module the worker process imports, not only the activities your workflows call. Importing a module adds the keys of all its rate-limited activities to the process, even if the worker never runs them.

Poll limit per worker process

Poll limit per worker process

Each internal worker keeps long-running polls open to the Workflows endpoint:

  • The main worker opens 5 workflow-task polls and 5 activity-task polls.
  • Each rate-limited and sticky-activity worker opens 5 activity-task polls only, because it registers no workflows.

A worker process sends all its calls over one connection, which allows 100 concurrent calls. With default settings, the poll count grows by 5 per key:

Distinct rate limit keysInternal workersConcurrent polls
0110
5635
121370
1920105

Add 5 polls if any activity sets sticky_to_worker=True.

When the process needs more than 100 concurrent calls, extra calls wait inside the worker. Workflow task results then miss their timeout and come back as NotFound errors, and workflows stop making progress. Task results and heartbeats share the same 100-call limit, so keep the poll count well below 100.

Choosing rate limit keys

Choosing rate limit keys

Use one key per upstream API:

  • Activities that call the same API share a key.
  • Activities that call different APIs keep separate keys.

Separate keys for the same API add internal workers and defeat the rate limit. Each key gets its own full budget, so two keys against one API allow twice the rate that API accepts.

# ❌ Avoid: two keys for the same upstream API.
# Each key adds an internal worker, and the API receives up to 200 calls/sec instead of 100.
@workflows.activity(rate_limit=workflows.RateLimit(time_window_in_sec=1, max_execution=100, key="ocr"))
async def run_ocr(params: OcrParams) -> OcrResult: ...

@workflows.activity(rate_limit=workflows.RateLimit(time_window_in_sec=1, max_execution=100, key="classify"))
async def classify_document(params: ClassifyParams) -> ClassifyResult: ...


# ✅ Prefer: one key per upstream API.
# Both activities call the Mistral API, so they share one key and one budget.
@workflows.activity(rate_limit=workflows.RateLimit(time_window_in_sec=1, max_execution=100, key="mistral_api"))
async def run_ocr(params: OcrParams) -> OcrResult: ...

@workflows.activity(rate_limit=workflows.RateLimit(time_window_in_sec=1, max_execution=100, key="mistral_api"))
async def classify_document(params: ClassifyParams) -> ClassifyResult: ...

# This activity calls a different API, so it keeps its own key.
@workflows.activity(rate_limit=workflows.RateLimit(time_window_in_sec=1, max_execution=5, key="crm_api"))
async def update_crm_record(params: CrmParams) -> None: ...

Give activities that share a key the same time_window_in_sec and max_execution. With different values, the SDK starts one internal worker per rate. Each worker polls the same task queue but registers only the activities with its rate. A task can reach a worker that doesn't have its activity and fail with is not registered on this worker.

Import only what a worker runs

Import only what a worker runs

If you run several worker entrypoints from one codebase, have each entrypoint import only the modules whose activities it runs. Avoid shared modules that import every activity in the project.

# ❌ Avoid: importing a module that pulls in every activity in the project.
# This worker registers the rate limit keys of activities it never runs.
from my_project import all_activities
from my_project.invoices.workflow import InvoiceWorkflow

# ✅ Prefer: import only the workflow and the activities it runs.
from my_project.invoices.workflow import InvoiceWorkflow  # imports my_project.invoices.activities


async def main():
    await workflows.run_worker([InvoiceWorkflow])

Give each entrypoint its own DEPLOYMENT_NAME. Workers in the same deployment poll the same task queues, so a worker can receive tasks for workflows or activities it doesn't register, and those tasks fail. Replicas of the same entrypoint, which register the same workflows and activities, can share a name.

Each deployment gets its own rate limit budget, even for the same key. If two deployments call the same upstream API, split that API's limit between them so their combined rate stays within it.

Lowering polls per worker

Lowering polls per worker

If you can't reduce the number of keys in a process, lower the number of activity-task polls each internal worker opens with the MAX_ACTIVITY_TASK_POLLERS environment variable (default 5, minimum 1):

MAX_ACTIVITY_TASK_POLLERS=2 uv run python my_worker.py

To set it for the whole shell session, run export MAX_ACTIVITY_TASK_POLLERS=2 before you start the worker. Without export, the worker process doesn't receive the variable and keeps the default. For a managed deployment, set it with ENV MAX_ACTIVITY_TASK_POLLERS=2 in your Dockerfile.

The setting applies to every internal worker in the process, including the main worker. A rate-limited worker receives tasks no faster than its configured rate, so it rarely needs more than 1 or 2 polls. Lower the value carefully if the main worker runs many activities without a rate limit, since fewer polls can reduce their throughput.