Deployment & Scaling
How to Deploy AI Agents with FastAPI and Redis Queue for Background Tasks
Long-running agent work may need to happen outside the code handling an API request. This guide shows the queue layer: how to use RQ and Redis to submit an agent function for asynchronous execution in a separate worker process.
The focus is deliberately narrow. You’ll set up the Redis connection and queue, then enqueue an importable Python function with its arguments.
The steps do not wire the queue into a FastAPI endpoint or cover deployment, health checks, or a general rule for choosing queued versus synchronous execution.
I’d use this walkthrough when you’re ready to hand agent work to a worker and want to understand the basic RQ enqueue path. You’ll need a supported Redis or Valkey service and an agent task that the worker can import.

Quick answer
The short version: install RQ in the Python environment that will enqueue jobs, connect to a compatible Redis or Valkey service, create an RQ Queue, and call enqueue with an importable agent function and the arguments it needs. RQ documents support for Redis 5 or newer, or Valkey 7.2 or newer, and gives pip install rq as its installation command.
The function is the work unit the queue hands to a worker; pass the function itself, not a call that runs it immediately. This setup covers submitting the job, not FastAPI endpoint wiring or a complete worker deployment.
It also doesn’t establish a universal cutoff for deciding when to queue work, or provide a health-check procedure.
In practice, check that the environment has RQ installed, the Redis connection is configured, and the queue receives the importable function with its required arguments. Those are the core pieces of the enqueue path described here.
Step-by-step guide
Requirements for the RQ queue
Before setting up the queue, make sure a compatible Redis service is available. RQ’s documented minimum is Redis 5 or newer; its documentation also supports Valkey 7.2 or newer.
You’ll also need the agent task as an importable Python function. RQ submits a function and its arguments to a queue for execution in a separate worker process, so the queue setup depends on having that function available to the worker.
This section covers the queue layer only. It doesn’t configure a FastAPI endpoint or prescribe how to deploy Redis or the worker.
Sources: RQ: Simple job queues for Python.
Install RQ
Activate the Python environment that will create and enqueue agent jobs. Install RQ there with:
pip install rqRun the command in your project’s environment, rather than a different system Python installation, so the package is available to the code that submits jobs. RQ is the queue library used to send Python functions and their arguments to a separate worker process; installing it adds the package, but does not by itself create a Redis connection or queue.
If installation completes, continue with the agent function and queue setup in that same environment. The Redis server must be available separately and meet RQ’s supported version requirements.
Sources: RQ: Simple job queues for Python.
Prepare the agent job function
Put the agent task in a Python module the worker can import, and define it as a function rather than wrapping it in an endpoint handler. For example, make the callable agent_jobs.run_agent(prompt) accept the inputs the task needs and call your existing agent logic.
Keep the function available to the worker under the same import path, and use arguments that can be passed to it when the job is enqueued. RQ runs submitted Python functions asynchronously in worker processes, so this function is the unit of work the queue will hand off.
Sources: RQ: Simple job queues for Python.
Create the Redis connection and queue
With RQ installed and your agent function ready to import, create a Redis connection and pass it to an RQ queue. This example assumes Redis is available on the same machine with its default port.
from redis import Redis
from rq import Queue
connection = Redis(host="localhost", port=6379, db=0)
agent_queue = Queue("agents", connection=connection)The queue name, agents, identifies the queue your jobs will be submitted to. If Redis runs elsewhere, use the connection settings for that service instead; keep the connection and queue setup in the Python environment that will enqueue the job.
Sources: RQ: Simple job queues for Python.
Enqueue the agent job
With the Redis connection and agent_queue from the previous step in place, import the agent function from its Python module and pass it to enqueue with the inputs it needs:
from agent_jobs import run_agent
job = agent_queue.enqueue(run_agent, "Summarize the latest report")Replace the example prompt with the arguments your function expects. Pass the function itself, run_agent, rather than calling it as run_agent(...); RQ needs the callable and its arguments to submit the work to the queue.
RQ runs the queued function asynchronously in a separate worker process, so the enqueueing code hands off the task instead of running the agent inline. To check that your enqueue path is configured as intended, confirm that it uses the importable function and the same arguments your task requires.
Sources: RQ: Simple job queues for Python.
Confirm the enqueue path is in place
Before checking whether the enqueue path is configured as intended, make sure the earlier steps are actually in place. The setup creates a Redis connection and an RQ Queue, then calls enqueue with an importable agent function and the arguments it needs.
If those elements exist in the right form, your agent jobs can be submitted to a separate worker process instead of running inline with the API request. You can verify each element independently so you know exactly where the configuration should be checked next.
Sources: RQ: Simple job queues for Python.
Check Redis compatibility and function importability
If an agent job is not being accepted or executed as expected, start with the two requirements covered here: Redis compatibility and an importable job function. RQ documents support for Redis 5 or newer, or Valkey 7.2 or newer.
Check which Redis or Valkey version your connection targets, then confirm the function you pass to enqueue can be imported by its Python module path. For example, if the function is agent_jobs.run_agent, the worker needs to be able to import run_agent from agent_jobs; pass the function itself, not a call that runs it immediately.
If either check fails, use a supported Redis or Valkey version or correct the function’s import path before retrying. The available documentation does not establish other failure causes or a health-check procedure, so avoid treating these checks as a complete diagnosis.
Sources: RQ: Simple job queues for Python.
Next steps
For the queue layer, the key check is straightforward: your code creates a Redis connection and an RQ Queue, then submits an importable agent function with the arguments it needs. If those pieces are in place, you have the enqueue path for handing the task to a separate worker process.
If a job does not enter the queue as expected, first confirm that the service meets RQ’s documented Redis or Valkey version requirement and that the worker can import the function you submit. These checks address the setup covered here; they are not a complete diagnosis or a substitute for deployment-specific health checks.
Once the enqueue path is configured, the next step is to connect it to your application and worker setup. Treat that as separate integration work: this guide establishes the RQ queue pattern, not a complete FastAPI deployment.