agentleFS
patonkikhpatonkikh/APES/skills/ai/ai-latency-optimizer/skill.md

ai-latency-optimizer

Skill3 starsChanged 4 months agoMITView on GitHub

Reduce AI inference latency: pipeline profiling, parallelization, caching, model selection, and streaming strategies to meet SLA targets. Use when working on prompt engineering, AI workflows, agents, context design, or LLM optimization.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Document synchronization point and timeout for slowest branch.

    line 70
  2. Distinctive:

    Use regional inference endpoints close to users.

    line 128

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
skill.md

AI Latency Optimizer

Purpose

Reduce AI inference latency: pipeline profiling, parallelization, caching, model selection, and streaming strategies to meet SLA targets.

Input: Workflow spec or architecture, latency SLA (p50/p95), current latency baseline (optional), quality constraints
Output: Latency Optimization Plan with bottleneck analysis, optimizations ranked by impact, and SLA compliance projection Examples: See examples.md for worked input/output.


Workflow

Step 1: Profile latency budget

Decompose end-to-end latency:

Stagep50 (ms)p95 (ms)% of total
Preprocessing
Retrieval (RAG)
LLM TTFT
LLM generation
Tool calls
Postprocessing

Identify bottleneck stage (>40% of p95 or exceeding individual SLA).

Step 2: Set SLA targets

MetricTargetMeasurement point
Time to first token (TTFT)Client-visible
End-to-end p50Client-visible
End-to-end p95Client-visible

Distinguish streaming (TTFT matters) vs batch (total time matters).

Step 3: Apply latency levers

LeverLatency impactQuality/cost trade-off
Parallel tool/retrieval callsHighNone if independent
Prompt/context reductionMediumPossible accuracy loss
Faster model tierHighQuality risk
Speculative decodingMediumHardware dependent
Edge/regional deploymentMediumInfra cost
Streaming responseHigh perceivedUX only
Precomputed embeddingsMediumStorage cost
Connection pooling / warm instancesMediumInfra cost

Step 4: Design parallel execution

For independent stages:

Input → [Retrieval || Tool prefetch || Classify] → Merge → Generate

Document synchronization point and timeout for slowest branch.

Step 5: Plan streaming strategy

Content typeStreamBuffer strategy
Long narrativeYesToken-by-token
Structured JSONPartialStream after schema validation
Tool callsNoWait for complete args

Step 6: Validate against SLA

Project p50/p95 after optimizations. Confirm quality metrics unchanged via eval gates.

Run Validation checklist.


Decision Rules

ConditionAction
SLA undefinedStop; request p50/p95 targets
Single stage >60% of p95Optimize bottleneck first before micro-optimizations
Parallelization adds race conditionsSerialize conflicting stages; parallelize only independent work
TTFT SLA <500ms with RAG + large modelFlag as high risk; recommend caching or smaller model
Streaming requested for JSON APIRecommend streaming tokens to UI only; API returns complete object
Optimization increases cost >20%Document trade-off; require explicit approval

Validation

  • Latency profile with per-stage p50/p95
  • Bottleneck stage identified
  • SLA targets documented (TTFT and e2e)
  • ≥3 optimizations evaluated with impact estimates
  • Parallel execution plan for independent stages
  • Streaming strategy defined for user-facing paths
  • Projected p95 meets SLA after changes
  • Quality impact assessed (no eval regression)

Anti-patterns

  • Optimizing non-bottleneck — shaving 10ms off postprocess when LLM takes 3s.
  • Cold start ignored — measuring warm-cache latency only.
  • Sequential by default — chaining retrieval and tools that could run in parallel.
  • Fake streaming — buffering full response then simulating stream.
  • Latency without monitoring — no production p95 tracking after deploy.

Best Practices

  • Measure client-side latency, not just server-side model time.
  • Set per-stage timeouts to fail fast and degrade gracefully.
  • Use regional inference endpoints close to users.
  • Pre-warm models/instances before traffic spikes.
  • Pair with ai-cost-optimizer when latency fixes increase spend.

Output Structure

# Latency Optimization Plan: [System Name]

## Current Profile
| Stage | p50 | p95 | % total |
|-------|-----|-----|---------|

## SLA Targets
| Metric | Target | Current | Gap |
|--------|--------|---------|-----|

## Bottleneck Analysis
[Root cause and contributing factors]

## Optimizations
| Lever | Est. p95 reduction | Trade-off | Priority |
|-------|-------------------|-----------|----------|

## Parallel Execution
[Diagram and sync points]

## Streaming Plan
[What streams, what waits]

## Projected Performance
| Metric | Before | After | Meets SLA |
|--------|--------|-------|-----------|

## Monitoring
| Metric | Source | Alert |
|--------|--------|-------|

Next Skills

OutcomeRecommended Skill
Re-validate qualityai/ai-evaluation-builder
Balance cost vs latencyai/ai-cost-optimizer
Redesign workflow structureai/ai-workflow-builder
Optimize context sizeai/context-engineering
Infra scalingarchitecture/scalability-advisor

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.