llm-integration
Skill33 starsChanged 22 days agoMITView on GitHub
Use when integrating an LLM API into an application. Covers streaming, retries and rate limits, timeouts, caching, fallback across providers, and the production concerns that a tutorial integration ignores. Trigger on "add an llm to our app", "getting 429 rate limit errors from the api", "stream the response to the frontend". Do not use for reducing token spend — see the llm-cost-optimization skill; for parsing JSON from the model, see the structured-output skill.
We split this skill into the rules it gives an agent
1 rule. 0 are common, 1 is distinctive, and 0 are very specific to this project.
0
Common
Most of the 7 edited versions we read keep these.
1
Distinctive
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
- Distinctive: line 55
Never call an LLM synchronously inside a request that has a tight latency budget.
“Common” is measured: kept by at least 6 in 10 of the 7 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
LLM Integration
Purpose
Integrate a language model into a production application, where the API is slow, rate-limited, occasionally down, and billed per token — none of which the quickstart mentions.
When to Use
- Adding an LLM to a production application.
- An LLM feature that is slow, expensive, or unreliable.
- Handling rate limits, streaming, or provider failover.
- Deciding where the model call belongs in the architecture.
Capabilities
- Streaming responses and partial rendering.
- Retry, backoff, and rate-limit handling.
- Timeouts and cancellation.
- Prompt caching and response caching.
- Multi-provider fallback.
- Token accounting and cost control.
Inputs
- The feature, its latency budget, and its cost budget.
- The provider's rate limits and their actual behavior under load.
- Whether the output is user-facing (stream it) or machine-consumed (do not).
Outputs
- A client with retries, timeouts, and a circuit breaker.
- Streaming where a user is waiting.
- Cost and latency instrumented per call.
Workflow
- Stream anything a human waits for — A 12-second response that starts rendering at 400ms feels fast. The same response delivered at once feels broken. Streaming is a perceived-latency fix, not a throughput one.
- Handle rate limits properly — Honor
Retry-After. Exponential backoff with jitter. A retry storm against a rate-limited endpoint extends the outage. - Set a timeout — LLM calls can hang. An unbounded call holds a connection and a worker until something else breaks.
- Cache the stable prefix — Prompt caching makes a large system prompt nearly free after the first call. This is often the largest single cost reduction available.
- Fail over deliberately — A second provider or a smaller model as a fallback. Decide in advance whether a degraded answer is better than no answer for this feature.
- Instrument tokens and cost per call — Attributed to the feature and the tenant. Without this, an LLM bill is an unexplainable number.
Best Practices
- Never call an LLM synchronously inside a request that has a tight latency budget. Stream it, or move it to a job and notify.
- The model will occasionally return something unusable. Every LLM call needs a defined behavior for "the output was garbage" — usually retry once, then fall back.
- Set
max_tokensdeliberately. Without it, a runaway generation costs money and time until it hits the model's own limit. - Retry on 429 and 5xx. Do not retry on 400 — the request is malformed and will be malformed again.
- Prompt caching requires a byte-identical prefix. A timestamp or a request ID at the top of the system prompt silently defeats it.
- Log the prompt, the response, and the token counts for a sample of calls. When quality degrades, this is the only evidence you will have.
Examples
A production client: timeout, retry, cache, fallback:
class LLMClient:
def __init__(self, primary: Provider, fallback: Provider | None = None):
self.primary = primary
self.fallback = fallback
self.breaker = CircuitBreaker(failure_threshold=5, reset_timeout=30)
async def complete(
self,
system: str,
messages: list[Message],
*,
max_tokens: int = 2048,
timeout: float = 60.0,
) -> Completion:
for attempt in range(3):
try:
if self.breaker.is_open:
break # skip straight to the fallback
async with asyncio.timeout(timeout):
result = await self.primary.complete(
system=[{
"type": "text",
"text": system,
"cache_control": {"type": "ephemeral"}, # cache the prefix
}],
messages=messages,
max_tokens=max_tokens,
)
self.breaker.record_success()
metrics.record(
provider="primary",
input_tokens=result.usage.input_tokens,
cached_tokens=result.usage.cache_read_input_tokens,
output_tokens=result.usage.output_tokens,
cost_cents=cost_of(result.usage),
)
return result
except RateLimitError as e:
# Honor the server's instruction. Do not invent your own backoff.
await asyncio.sleep(e.retry_after or (2 ** attempt) + random.random())
except (APIError, TimeoutError) as e:
self.breaker.record_failure()
if attempt == 2:
break
await asyncio.sleep((2 ** attempt) + random.random())
except BadRequestError:
raise # malformed: retrying changes nothing
if self.fallback:
logger.warning("primary_llm_unavailable_using_fallback")
return await self.fallback.complete(system=system, messages=messages,
max_tokens=max_tokens)
raise LLMUnavailable("primary failed and no fallback is configured")Streaming to the user while accumulating for storage:
async def stream_answer(question: str) -> AsyncIterator[str]:
buffer = []
async with client.stream(question) as stream:
async for chunk in stream:
buffer.append(chunk.text)
yield chunk.text # to the user, immediately
await store_answer("".join(buffer)) # complete, after the stream closesNotes
- Prompt caching typically reduces the cost of the cached portion by around 90% and reduces time-to-first-token substantially. A stable, cached system prompt is worth restructuring the code for.
- A circuit breaker matters more for LLM providers than for most dependencies: when they degrade, they degrade for everyone at once, and retrying through a provider-wide incident just adds latency to a certain failure.
- Cost attribution per tenant is not optional in a multi-tenant product. Without it, one customer's usage pattern can quietly consume the margin on the whole product.
Other versions9 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 9
- rapidaai/voice-ai★ 750same name
- ehnruiz/claude-skills★ 3same name
- blackbenjamin/etps★ 1same name
- medy-gribkov/arcana★ 1same name
- SkotAlexsander/skills★ 1same name
- MrMaca11an/claude-skills-for-crypto-quant★ 0same name
- ThuPhuong1010/claude-factory★ 0same name
- yui-synth-lab/obsidian-weaklog-processor★ 0same name
- zeeshanalimughal/claude-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository137 other files it gives its agents.
- agent-instructionsSkill
- agent-memorySkill
- hooksSkill
- plugin-developmentSkill
- skill-authoringSkill
- skill-reviewSkill
- slash-commandsSkill
- subagentsSkill
- agent-designSkill
- context-engineeringSkill
- fine-tuningSkill
- llm-cost-optimizationSkill
- llm-evaluationSkill
- mcp-serverSkill
- ml-pipelineSkill
- model-selectionSkill
- prompt-engineeringSkill
- ragSkill
- structured-outputSkill
- api-designSkill
- background-jobsSkill
- cachingSkill
- djangoSkill
- event-driven-architectureSkill
- fastapiSkill
- graphqlSkill
- microservicesSkill
- nestjsSkill
- realtime-websocketsSkill
- spring-bootSkill
- competitive-analysisSkill
- lead-researchSkill
- product-analysisSkill
- resumeSkill
- user-researchSkill
- database-performanceSkill
- data-modelingSkill
- data-qualitySkill
- pandasSkill
- postgresSkill
- sparkSkill
- brand-guidelinesSkill
- generative-artSkill
- themingSkill
- ui-reviewSkill
- visual-designSkill
- architecture-decisionsSkill
- bug-fix-protocolSkill
- cli-developmentSkill
- code-reviewSkill
- debuggingSkill
- design-patternsSkill
- domain-modelingSkill
- git-workflowSkill
- legacy-modernizationSkill
- refactoringSkill
- repository-explorationSkill
- simple-designSkill
- system-designSkill
- aws-cdkSkill
- The first 60 shown.
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
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.

