# RAG Pipeline Latency Profiler - SLO & Bottleneck Audit (`quanmatrix/rag-pipeline-latency-profiler`) Actor

Profile RAG traces with p50/p90/p95/p99, error rates, SLO breaches, bottleneck ranking and instrumentation gaps across arbitrary retrieval, reranking and generation stages.

- **URL**: https://apify.com/quanmatrix/rag-pipeline-latency-profiler.md
- **Developed by:** [Rafael Barreto Haddad](https://apify.com/quanmatrix) (community)
- **Categories:** Developer tools
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $14.00 / 1,000 results

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.
Since this Actor supports Apify Store discounts, the price gets lower the higher subscription plan you have.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

Actors are web data automations that power AI and operations. They run on the Apify platform to scrape websites, process data, connect APIs, and automate workflows.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

- **AI agents and MCP clients** — the [Apify MCP server](https://docs.apify.com/integrations/mcp.md) at `https://mcp.apify.com` (remote, streamable HTTP, OAuth on first use).
- **Agentic workflows and local Actor development** — [Agent Skills](https://apify.com/.well-known/agent-skills/index.json) with the [Apify CLI](https://docs.apify.com/cli/docs.md): `npm install -g apify-cli`, then `apify login`.
- **JavaScript/TypeScript projects** — the official [JS/TS client](https://docs.apify.com/api/client/js/docs.md): `npm install apify-client`.
- **Python projects** — the official [Python client](https://docs.apify.com/api/client/python/docs.md): `pip install apify-client`.
- **Any other language** — the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).

# README

## RAG Pipeline Latency Profiler

Stage-by-stage performance, tail latency, bottleneck ranking, SLO breaches, and instrumentation gaps for Retrieval-Augmented Generation pipelines.

This Actor turns RAG trace rows into a compact performance audit. It supports arbitrary pipeline stages and calculates p50, p90, p95, and p99 latency, error rate, observed latency share, end-to-end request latency when request IDs are available, and optimization priorities for the slowest stages.

### Use cases

- production RAG observability
- weekly latency regression checks
- vector retrieval and reranking tuning
- LLM gateway and model-routing optimization
- AI platform SRE and FinOps teams
- pre-release performance baselines
- comparing pipeline changes before and after deployment

### Why use this Actor

### Key features

- p50, p90, p95, and p99 latency by arbitrary pipeline stage
- Request-level end-to-end timing when request IDs are available
- Stage and end-to-end SLO breach analysis
- Bottleneck ranking, instrumentation gaps, and explainable optimization priorities

An average latency number hides the problem engineers usually care about: tail behavior and where the time actually goes. This Actor reports:

- average, min, max, p50, p90, p95, and p99 by stage
- stage error rate
- share of observed span time by stage
- per-stage SLO breach rate when thresholds are supplied
- measured end-to-end request percentiles when `request_id` is present
- a ranked bottleneck list
- stage-specific optimization suggestions
- missing instrumentation and low-sample warnings

No external model, paid API, vector database, or browser is required. The Actor analyzes telemetry supplied in the input, keeping compute and platform cost low.

### Input

`rag_logs` is required. Each row can be a JSON object or a JSON string. Every valid row needs at least:

- `stage`
- `latency_ms`

For stronger analysis, add `request_id`, `status`, or `success`.

### Example

```json
{
  "rag_logs": [
    {"request_id": "req-1", "stage": "embedding", "latency_ms": 82, "status": "ok"},
    {"request_id": "req-1", "stage": "retrieval", "latency_ms": 238, "status": "ok"},
    {"request_id": "req-1", "stage": "reranking", "latency_ms": 144, "status": "ok"},
    {"request_id": "req-1", "stage": "generation", "latency_ms": 1180, "status": "ok"}
  ],
  "expected_stages": ["embedding", "retrieval", "reranking", "generation"],
  "end_to_end_sla_ms": 2200,
  "stage_sla_ms": {
    "retrieval": 350,
    "generation": 1500
  }
}
```

#### Arbitrary stages

The profiler is not limited to the classic retrieval/embedding/generation trio. You can instrument stages such as:

- query rewriting
- cache lookup
- hybrid search
- graph traversal
- metadata filtering
- reranking
- prompt assembly
- guardrails
- model generation
- citation validation

Stage names are preserved exactly in the output.

### End-to-end measurement

When spans include `request_id`, the Actor sums stage latency per request and reports measured request-level p50/p90/p95/p99 and SLO breach rate.

When request IDs are absent, the Actor reports only a clearly labelled additive estimate based on stage averages. It does not pretend that an inferred average is a measured end-to-end percentile.

### Output

A run emits **one default-dataset item** containing the complete audit:

- `stage_profiles`
- `bottleneck_ranking`
- `end_to_end`
- `recommendations`
- `instrumentation_gaps`
- evidence confidence
- method note explaining measured versus estimated metrics

The same result is written to the `OUTPUT` key-value-store record.

### Limitations

#### Responsible interpretation

Tail percentiles are unstable with very small samples. The Actor flags low-sample stages and reports confidence rather than dressing four traces in a lab coat and calling them production science.

Optimization suggestions are hypotheses to benchmark, not claims that a specific infrastructure change will always improve performance.

### Data provenance and privacy

All calculations use only the trace rows you provide. The Actor does not send your telemetry to external model providers. Avoid including prompts, retrieved documents, secrets, or personal data when stage metadata and timings are sufficient.

### Pricing

Pricing is pay per completed audit: one successfully emitted performance audit equals one billable default-dataset item. The Store price is activated only after the factory checks real Apify validation-run cost and margin.

### Search terms

RAG latency profiler, RAG observability, RAG bottleneck analyzer, RAG performance benchmark, retrieval latency, LLM latency audit, RAG p95 p99, vector search performance, AI pipeline SLO, RAG monitoring.

### Commercial intelligence layer

Gen2 adds baseline latency regression, projected request volume, optional infrastructure-cost impact, confidence scoring and a **GO / WARN / BLOCK** release decision on top of the existing stage percentiles, bottleneck ranking and SLO evidence.

# Actor input Schema

## `rag_logs` (type: `array`):

Each row can be an object or JSON string with at least stage and latency\_ms.

## `expected_stages` (type: `array`):

Used to flag missing instrumentation. Arbitrary stage names remain supported.

## `end_to_end_sla_ms` (type: `number`):

Optional latency target. Measured breach rate requires request\_id on spans.

## `stage_sla_ms` (type: `object`):

Optional mapping from stage name to latency target in milliseconds.

## `previous_profile` (type: `object`):

Optional prior Actor output for release-regression comparison.

## `monthly_requests` (type: `integer`):

Projected monthly request volume for economic impact estimates.

## `cost_per_request_second_usd` (type: `number`):

Optional effective infrastructure/provider cost per request-second.

## `warn_sla_breach_rate` (type: `number`):

Warn SLA breach rate. Configure this optional control for the current audit when needed.

## `block_sla_breach_rate` (type: `number`):

Block SLA breach rate. Configure this optional control for the current audit when needed.

## Actor input object example

```json
{
  "rag_logs": [
    {
      "request_id": "r1",
      "stage": "embedding",
      "latency_ms": 80,
      "status": "ok"
    },
    {
      "request_id": "r1",
      "stage": "retrieval",
      "latency_ms": 240,
      "status": "ok"
    },
    {
      "request_id": "r1",
      "stage": "reranking",
      "latency_ms": 150,
      "status": "ok"
    },
    {
      "request_id": "r1",
      "stage": "generation",
      "latency_ms": 1100,
      "status": "ok"
    }
  ],
  "expected_stages": [
    "retrieval",
    "embedding",
    "reranking",
    "generation"
  ],
  "previous_profile": {},
  "monthly_requests": 0,
  "cost_per_request_second_usd": 0,
  "warn_sla_breach_rate": 0.05,
  "block_sla_breach_rate": 0.25
}
```

# Actor output Schema

## `results` (type: `string`):

Stage percentiles, bottleneck ranking, SLO analysis and instrumentation gaps.

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "rag_logs": [
        {
            "request_id": "r1",
            "stage": "embedding",
            "latency_ms": 80,
            "status": "ok"
        },
        {
            "request_id": "r1",
            "stage": "retrieval",
            "latency_ms": 240,
            "status": "ok"
        },
        {
            "request_id": "r1",
            "stage": "reranking",
            "latency_ms": 150,
            "status": "ok"
        },
        {
            "request_id": "r1",
            "stage": "generation",
            "latency_ms": 1100,
            "status": "ok"
        }
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("quanmatrix/rag-pipeline-latency-profiler").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "rag_logs": [
        {
            "request_id": "r1",
            "stage": "embedding",
            "latency_ms": 80,
            "status": "ok",
        },
        {
            "request_id": "r1",
            "stage": "retrieval",
            "latency_ms": 240,
            "status": "ok",
        },
        {
            "request_id": "r1",
            "stage": "reranking",
            "latency_ms": 150,
            "status": "ok",
        },
        {
            "request_id": "r1",
            "stage": "generation",
            "latency_ms": 1100,
            "status": "ok",
        },
    ] }

# Run the Actor and wait for it to finish
run = client.actor("quanmatrix/rag-pipeline-latency-profiler").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "rag_logs": [
    {
      "request_id": "r1",
      "stage": "embedding",
      "latency_ms": 80,
      "status": "ok"
    },
    {
      "request_id": "r1",
      "stage": "retrieval",
      "latency_ms": 240,
      "status": "ok"
    },
    {
      "request_id": "r1",
      "stage": "reranking",
      "latency_ms": 150,
      "status": "ok"
    },
    {
      "request_id": "r1",
      "stage": "generation",
      "latency_ms": 1100,
      "status": "ok"
    }
  ]
}' |
apify call quanmatrix/rag-pipeline-latency-profiler --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,quanmatrix/rag-pipeline-latency-profiler"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/Gbgd4TryQU9xJexj5/builds/sqqdxkBq3bHkoQloL/openapi.json
