
Api Response Optimization
- 438 installs
- 305 repo stars
- Updated March 4, 2026
- aj-geddes/useful-ai-prompts
api-response-optimization is an agent skill that reduces API latency and payload size through field selection, compression, caching headers, N+1 query fixes, and lean serialization for developers optimizing production-bo
About
api-response-optimization is a useful-ai-prompts agent skill focused on faster API responses through payload trimming, caching, compression, and query efficiency. The quick-start contrasts bloated user JSON exposing password_hash, SSN, and unused nested geo fields against a minimal id-name-email response shape. Four reference guides cover response-payload-optimization, caching-strategies, compression-performance, and an optimization-checklist with concrete implementation patterns. Developers reach for api-response-optimization when endpoints show high CPU, large response bodies, scaling bottlenecks, or measurable latency regressions before production load. The skill emphasizes selecting only required GraphQL or REST fields, setting Cache-Control and ETag headers, enabling gzip or Brotli compression, and eliminating N+1 database access patterns. Best practices warn against sending sensitive columns, shipping unused metadata, and skipping response-size monitoring during load tests.
- Trims over-fetching and bloated DTOs
- Applies compression and cache headers
- Resolves N+1 and slow query hotspots
- Benchmarks payload and latency wins
Api Response Optimization by the numbers
- 438 all-time installs (skills.sh)
- Ranked #999 of 4,347 Backend & APIs skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill api-response-optimizationAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 438 |
|---|---|
| repo stars | ★ 305 |
| Last updated | March 4, 2026 |
| Repository | aj-geddes/useful-ai-prompts ↗ |
How do you reduce API response payload size and latency?
Reduce API latency and payload size via field selection, compression, caching headers, N+1 query fixes, and lean serialization before production traffic.
Who is it for?
Backend developers profiling slow REST or GraphQL endpoints who need structured guidance on payload trimming, HTTP caching, compression, and query efficiency before launch.
Skip if: Greenfield API design with no performance symptoms or teams optimizing database indexes alone without HTTP response-layer changes.
When should I use this skill?
The user reports slow API responses, large JSON payloads, high server CPU on read endpoints, or scaling bottlenecks tied to response size and caching.
What you get
Lean API response schemas, caching and compression headers, N+1 query fixes, and a completed optimization checklist for target endpoints.
- Trimmed response DTO schemas
- Caching and compression header configuration
- Completed optimization checklist per endpoint
By the numbers
- Bundles 4 reference implementation guides
- Quick-start demonstrates trimming 8+ unnecessary user JSON fields
Files
API Response Optimization
Table of Contents
Overview
Fast API responses improve overall application performance and user experience. Optimization focuses on payload size, caching, and query efficiency.
When to Use
- Slow API response times
- High server CPU/memory usage
- Large response payloads
- Performance degradation
- Scaling bottlenecks
Quick Start
Minimal working example:
// Inefficient response (unnecessary data)
GET /api/users/123
{
"id": 123,
"name": "John",
"email": "john@example.com",
"password_hash": "...", // ❌ Should never send
"ssn": "123-45-6789", // ❌ Sensitive data
"internal_id": "xyz",
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-02T00:00:00Z",
"meta_data": {...}, // ❌ Unused fields
"address": {
"street": "123 Main",
"city": "City",
"state": "ST",
"zip": "12345",
"geo": {...} // ❌ Not needed
}
}
// Optimized response (only needed fields)
GET /api/users/123
{
"id": 123,
// ... (see reference guides for full implementation)Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Response Payload Optimization | Response Payload Optimization |
| Caching Strategies | Caching Strategies |
| Compression & Performance | Compression & Performance |
| Optimization Checklist | Optimization Checklist |
Best Practices
✅ DO
- Follow established patterns and conventions
- Write clean, maintainable code
- Add appropriate documentation
- Test thoroughly before deploying
❌ DON'T
- Skip testing or validation
- Ignore error handling
- Hard-code configuration values
Caching Strategies
Caching Strategies
HTTP Caching Headers:
Cache-Control:
Immutable assets: Cache-Control: public, max-age=31536000
API responses: Cache-Control: private, max-age=300
No cache: Cache-Control: no-store
Revalidate: Cache-Control: max-age=0, must-revalidate
ETag:
- Unique identifier for response version
- If-None-Match: return 304 if unchanged
- Saves bandwidth on unchanged data
Last-Modified:
- If-Modified-Since: return 304 if unchanged
- Simple versioning mechanism
---
Application-Level Caching:
Database Query Caching:
- Cache expensive queries
- TTL: 5-30 minutes
- Invalidate on write
- Tools: Redis, Memcached
Response Caching:
- Cache entire API responses
- Use Cache-Control headers
- Key: URL + query params
- TTL: Based on data freshness
Fragment Caching:
- Cache parts of response
- Combine multiple fragments
- Different TTL per fragment
---
Cache Invalidation:
Time-based (TTL):
- Simple: expires after time
- Risk: stale data
- Best for: Non-critical data
Event-based:
- Invalidate on write
- Immediate freshness
- Requires coordination
Hybrid:
- TTL + event invalidation
- Short TTL + invalidate on change
- Good balance
---
Implementation Example:
GET /api/users/123/orders
Authorization: Bearer token
Cache-Control: public, max-age=300
Response:
HTTP/1.1 200 OK
Cache-Control: public, max-age=300
ETag: "123abc"
Last-Modified: 2024-01-01
{data: [...]}
-- Next request within 5 minutes from cache
-- After 5 minutes, revalidate with ETag
-- If unchanged: 304 Not ModifiedCompression & Performance
Compression & Performance
Compression:
gzip:
Ratio: 60-80% reduction
Format: text/html, application/json
Overhead: CPU (minor)
brotli:
Ratio: 20% better than gzip
Support: Modern browsers (95%)
Overhead: Higher CPU
Implementation:
- Enable in server
- Set Accept-Encoding headers
- Measure: Before/after sizes
- Monitor: CPU impact
---
Performance Optimization:
Pagination:
- Limit: 20-100 items per request
- Offset pagination: Simple, slow for large offsets
- Cursor pagination: Efficient, stable
- Implementation: Always use limit
Filtering:
- Server-side filtering
- Reduce response size
- Example: ?status=active
Sorting:
- Server-side only
- Index frequently sorted fields
- Limit sort keys to 1-2 fields
Eager Loading:
- Fetch related data in one query
- Avoid N+1 problem
- Example: /users?include=posts
---
Metrics & Monitoring:
Track:
- API response time (target: <200ms)
- Payload size (target: <100KB)
- Cache hit rate (target: >80%)
- Server CPU/memory
Tools:
- New Relic APM
- DataDog
- Prometheus
- Custom logging
Setup alerts:
- Response time >500ms
- Payload >500KB
- Cache miss spike
- Error ratesOptimization Checklist
Optimization Checklist
Payload:
[ ] Remove sensitive data
[ ] Remove unused fields
[ ] Implement sparse fieldsets
[ ] Compress payload
[ ] Use appropriate status codes
Caching:
[ ] HTTP caching headers set
[ ] ETags implemented
[ ] Application cache configured
[ ] Cache invalidation strategy
[ ] Cache monitoring
Query Efficiency:
[ ] Database queries optimized
[ ] N+1 queries fixed
[ ] Joins optimized
[ ] Indexes in place
Compression:
[ ] gzip enabled
[ ] brotli enabled (modern)
[ ] Accept-Encoding headers
[ ] Content-Encoding responses
Monitoring:
[ ] Response time tracked
[ ] Payload size tracked
[ ] Cache metrics
[ ] Error rates
[ ] Alerts configured
Expected Improvements:
- Response time: 500ms → 100ms
- Payload size: 500KB → 50KB
- Server load: 80% CPU → 30%
- Concurrent users: 100 → 1000Response Payload Optimization
Response Payload Optimization
// Inefficient response (unnecessary data)
GET /api/users/123
{
"id": 123,
"name": "John",
"email": "john@example.com",
"password_hash": "...", // ❌ Should never send
"ssn": "123-45-6789", // ❌ Sensitive data
"internal_id": "xyz",
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-02T00:00:00Z",
"meta_data": {...}, // ❌ Unused fields
"address": {
"street": "123 Main",
"city": "City",
"state": "ST",
"zip": "12345",
"geo": {...} // ❌ Not needed
}
}
// Optimized response (only needed fields)
GET /api/users/123
{
"id": 123,
"name": "John",
"email": "john@example.com"
}
// Results: 2KB → 100 bytes (20x smaller)
// Sparse fieldsets pattern
GET /api/users/123?fields=name,email
{
"id": 123,
"name": "John",
"email": "john@example.com"
}#!/bin/bash
# validate-api.sh - Validate API specification
# Usage: ./validate-api.sh <openapi_spec>
set -euo pipefail
SPEC_FILE="${{1:?Usage: $0 <openapi_spec>}}"
echo "Validating API spec: $SPEC_FILE"
# TODO: Add API validation
# - Validate OpenAPI/Swagger syntax
# - Check endpoint naming conventions
# - Verify response schemas
# - Check for required headers
# - Validate authentication definitions
echo "API validation complete."
# API Endpoint Scaffold
# TODO: Customize for your API framework
openapi: "3.0.3"
info:
title: "API Service"
version: "1.0.0"
paths:
/api/v1/resource:
get:
summary: "List resources"
# TODO: Define parameters and responses
responses:
"200":
description: "Success"
post:
summary: "Create resource"
# TODO: Define request body and responses
responses:
"201":
description: "Created"
Related skills
How it compares
Use api-response-optimization for HTTP response-layer tuning; pair with database indexing skills when latency is query-plan bound rather than payload or cache bound.
FAQ
What problems does api-response-optimization address?
api-response-optimization targets slow API response times, high server CPU or memory on read endpoints, oversized JSON payloads, and scaling bottlenecks caused by sending unnecessary fields or missing cache and compression headers.
Which reference guides ship with api-response-optimization?
api-response-optimization bundles four references: response-payload-optimization, caching-strategies, compression-performance, and optimization-checklist. Each contains implementation patterns beyond the quick-start before/after JSON example.
What is the first payload change recommended?
api-response-optimization starts by returning only required fields per endpoint, removing sensitive columns like password_hash or SSN and dropping unused nested metadata such as geo objects not consumed by clients.