Llm integrator

From FerroxLabs/wayland · View interactive version

Llm integrator is an AI agent skill from the GitHub repository FerroxLabs/wayland. LLM API integration covering OpenAI, Anthropic, and Google API patterns, streaming responses, function calling and tool use, token counting, cost optimization, fallback strategies, rate limit handling, response caching, and multi-model routing. Use when the user asks about llm integrator, llm integr

How to use this skill

Skill file

LLM Integrator

Overview

Integrating LLMs into production applications requires robust API handling, cost management, and reliability patterns. This skill covers practical integration with major LLM providers, including streaming, tool use, error handling, and multi-model architectures for production systems.

Provider API Patterns

OpenAI

from openai import OpenAI, AsyncOpenAI

client = OpenAI(api_key=environment-variables["OPENAI_API_KEY"])

# Basic completion
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum computing."},
    ],
    # ... (condensed) ...
        for p in prompts
    ]
    responses = await asyncio.gather(*tasks)
    return [r.choices[0].message.content for r in responses]

Anthropic

from anthropic import Anthropic, AsyncAnthropic

client = Anthropic(api_key=environment-variables["ANTHROPIC_API_KEY"])

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are an expert software architect.",
    messages=[
        {"role": "user", "content": "Design a microservices architecture for e-commerce."},
    ],
# ... (condensed) ...
    if block.type == "thinking":
        print(f"Thinking: {block.thinking}")
    elif block.type == "text":
        print(f"Answer: {block.text}")

Google Gemini

import google.generativeai as genai

genai.configure(api_key=environment-variables["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-2.0-flash")

response = model.generate_content("Explain neural networks.")
print(response.text)

# Multi-modal input
import PIL.Image
image = PIL.Image.open("diagram.png")
response = model.generate_content(["Describe this diagram:", image])
print(response.text)

Streaming Responses

OpenAI Streaming

def stream_openai(prompt: str):
    """Stream response tokens as they are generated."""
    stream = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        stream=True,
    )

    full_response = ""
    for chunk in stream:
        delta = chunk.choices[0].delta.content
        if delta:
            full_response += delta
            print(delta, end="", flush=True)

    return full_response

Anthropic Streaming

def stream_anthropic(prompt: str):
    """Stream with Anthropic's event-based API."""
    with client.messages.stream(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": prompt}],
    ) as stream:
        full_response = ""
        for text in stream.text_stream:
            full_response += text
            print(text, end="", flush=True)

    return full_response

Server-Sent Events (SSE) for Web Apps

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()

@app.get("/chat/stream")
async def chat_stream(prompt: str):
    async def event_generator():
        async with async_client.messages.stream(
            model="claude-sonnet-4-20250514",
            max_tokens=1024,
            # ... (condensed) ...
    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
    )

Function Calling / Tool Use

OpenAI Function Calling

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        # ... (condensed) ...
                    "content": json.dumps(result),
                })
        else:
            return message.content

Anthropic Tool Use

tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City and state"
                }
            # ... (condensed) ...
            messages.append({"role": "assistant", "content": response.content})
            messages.append({"role": "user", "content": tool_results})
        else:
            return next(b.text for b in response.content if b.type == "text")

Token Counting and Cost Management

Token Counting

import tiktoken

class TokenCounter:
    """Count tokens for OpenAI models."""

    COST_PER_1M = {
        "gpt-4o":       {"input": 2.50, "output": 10.00},
        "gpt-4o-mini":  {"input": 0.15, "output": 0.60},
        "gpt-4-turbo":  {"input": 10.00, "output": 30.00},
    }

    # ... (condensed) ...
        return (
            input_tokens * costs["input"] / 1_000_000 +
            output_tokens * costs["output"] / 1_000_000
        )

Cost Optimization Strategies

class CostOptimizedClient:
    """Route requests to minimize cost while maintaining quality."""

    def __init__(self):
        self.openai = OpenAI()
        self.anthropic = Anthropic()
        self.token_counter = TokenCounter()

    def complete(
        self,
        prompt: str,
        # ... (condensed) ...
            for kw in ["architecture", "complex", "multi-step"]
        ):
            return "complex"
        return "medium"

Rate Limit Handling

Retry with Exponential Backoff

import time
from tenacity import (
    retry, stop_after_attempt, wait_exponential,
    retry_if_exception_type,
)
from openai import RateLimitError, APITimeoutError, InternalServerError

@retry(
    retry=retry_if_exception_type((RateLimitError, APITimeoutError, InternalServerError)),
    wait=wait_exponential(multiplier=1, min=1, max=60),
    stop=stop_after_attempt(6),
    # ... (condensed) ...
)
def resilient_completion(client, **kwargs):
    """LLM call with automatic retry on transient errors."""
    return client.chat.completions.create(**kwargs)

Token Bucket Rate Limiter

import asyncio
import time

class TokenBucketRateLimiter:
    """Rate limiter for API calls respecting tokens-per-minute limits."""

    def __init__(self, tokens_per_minute: int, requests_per_minute: int):
        self.tpm = tokens_per_minute
        self.rpm = requests_per_minute
        self.token_bucket = tokens_per_minute
        self.request_bucket = requests_per_minute
        # ... (condensed) ...
            self.rpm,
            self.request_bucket + elapsed * self.rpm / 60
        )
        self.last_refill = now

Fallback Strategies

Multi-Provider Fallback

class LLMRouter:
    """Route LLM calls with automatic fallback across providers."""

    def __init__(self):
        self.providers = [
            {
                "name": "openai",
                "client": OpenAI(),
                "model": "gpt-4o",
                "priority": 1,
                "healthy": True,
                # ... (condensed) ...
                max_tokens=kwargs.get("max_tokens", 1024),
                messages=messages,
            )
            return resp.content[0].text

Response Caching

Semantic Cache

import hashlib
import json
import redis

class LLMCache:
    """Cache LLM responses with exact matching."""

    def __init__(self, redis_url: str, ttl: int = 86400):
        self.redis = redis.from_url(redis_url)
        self.ttl = ttl

    # ... (condensed) ...
        result = response.choices[0].message.content

        self.set(messages, model, result)
        return result

Multi-Model Routing

Complexity-Based Router

class SmartRouter:
    """Route requests to optimal model based on task analysis."""

    MODEL_CONFIGS = {
        "fast_cheap": {
            "provider": "openai",
            "model": "gpt-4o-mini",
            "max_tokens": 1024,
            "cost_input": 0.15,
            "cost_output": 0.60,
        },
        # ... (condensed) ...
        )
        classification = resp.choices[0].message.content.strip().lower()
        mapping = {"simple": "fast_cheap", "medium": "balanced", "complex": "max_quality"}
        return mapping.get(classification, "balanced")

Production Architecture

Complete Integration Layer

class LLMService:
    """Production-ready LLM service with all reliability patterns."""

    def __init__(self, config: dict):
        self.router = SmartRouter()
        self.cache = LLMCache(config["redis_url"])
        self.rate_limiter = TokenBucketRateLimiter(
            tokens_per_minute=config["tpm"],
            requests_per_minute=config["rpm"],
        )
        self.fallback = LLMRouter()
        # ... (condensed) ...
        self.metrics.record("latency", latency)
        self.metrics.record("model_used", config["model"])

        return result

Checklist

When to Use

Use this skill when:

Do NOT use this skill when:

Output Format

# Llm Integrator Analysis

## Context Assessment
[Situation summary and constraints]

## Recommended Approach
[Primary recommendation with rationale]

## Implementation Steps
1. [Step with specific details]
2. [Step with specific details]
3. [Step with specific details]

## Trade-offs and Considerations
- [Key trade-off 1]
- [Key trade-off 2]

## Next Steps
- [Immediate action item]
- [Follow-up action item]

Example

Input: "Help me implement llm integrator for a medium-scale production application"

Output: A structured analysis covering current state assessment, recommended llm integrator approach with specific patterns, implementation roadmap with milestones, and risk mitigation strategies tailored to the application scale and constraints.

Edge Cases

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.