LLM Integration Skill

SkillAI & models

LLM integration patterns for Claude, GPT, Gemini, and Ollama. Activate for AI API integration, prompt engineering, token management, extended thinking, and multi-model orchestration.

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the LLM Integration Skill skill

What this skill tells your AI

The instructions your AI receives, as published by thelobbi/claude in .claude/skills/llm-integration/SKILL.md and read by ahel’s review.

Provides comprehensive LLM integration capabilities for the Golden Armada AI Agent Fleet Platform, including advanced features like extended thinking, sophisticated prompt engineering, and intelligent token budget management.

When to Use This Skill

Activate this skill when working with:

  • Claude/Anthropic API integration
  • OpenAI GPT integration
  • Google Gemini integration
  • Ollama local models
  • Multi-model orchestration
  • Prompt engineering

Anthropic Claude Integration

```python import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

Basic completion

message = client.messages.create( model="claude-sonnet-5", max_tokens=1024, messages=[ {"role": "user", "content": "Hello, Claude!"} ] ) print(message.content[0].text)

With system prompt

message = client.messages.create( model="claude-sonnet-5", max_tokens=1024, system="You are a helpful coding assistant.", messages=[ {"role": "user", "content": "Write a Python function to sort a list."} ] )

Streaming

with client.messages.stream( model="claude-sonnet-5", max_tokens=1024, messages=[{"role": "user", "content": "Tell me a story."}] ) as stream: for text in stream.text_stream: print(text, end="", flush=True)

Tool use

tools = [ { "name": "get_weather", "description": "Get the current weather in a location", "input_schema": { "type": "object", "properties": { "location": {"type": "string", "description": "The city and state"} }, "required": ["location"] } } ]

message = client.messages.create( model="claude-sonnet-5", max_tokens=1024, tools=tools, messages=[{"role": "user", "content": "What's the weather in San Francisco?"}] ) ```

OpenAI GPT Integration

```python from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

Basic completion

response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ] ) print(response.choices[0].message.content)

Streaming

stream = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Write a poem."}], stream=True ) for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")

Function calling

functions = [ { "name": "get_weather", "description": "Get the current weather", "parameters": { "type": "object", "properties": { "location": {"type": "string"} }, "required": ["location"] } } ]

response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Weather in NYC?"}], functions=functions, function_call="auto" ) ```

Google Gemini Integration

```python import google.generativeai as genai

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

model = genai.GenerativeModel('gemini-pro')

Basic generation

response = model.generate_content("Explain quantum computing") print(response.text)

Chat

chat = model.start_chat(history=[]) response = chat.send_message("Hello!") print(response.text)

Streaming

response = model.generate_content("Tell me a story", stream=True) for chunk in response: print(chunk.text, end="") ```

Ollama Local Models

```python import ollama

Basic completion

response = ollama.chat( model='llama2', messages=[ {'role': 'user', 'content': 'Hello!'} ] ) print(response['message']['content'])

Streaming

stream = ollama.chat( model='llama2', messages=[{'role': 'user', 'content': 'Tell me a story.'}], stream=True ) for chunk in stream: print(chunk['message']['content'], end='')

Pull model

ollama.pull('llama2')

List models

models = ollama.list() ```

Multi-Model Abstraction

```python from abc import ABC, abstractmethod from typing import Generator

class LLMProvider(ABC): @abstractmethod def generate(self, prompt: str, **kwargs) -> str: pass

@abstractmethod
def stream(self, prompt: str, **kwargs) -> Generator[str, None, None]:
    pass

class ClaudeProvider(LLMProvider): def init(self, api_key: str, model: str = "claude-sonnet-5"): self.client = anthropic.Anthropic(api_key=api_key) self.model = model

def generate(self, prompt: str, **kwargs) -> str:
    message = self.client.messages.create(
        model=self.model,
        max_tokens=kwargs.get('max_tokens', 1024),
        messages=[{"role": "user", "content": prompt}]
    )
    return message.content[0].text

def stream(self, prompt: str, **kwargs) -> Generator[str, None, None]:
    with self.client.messages.stream(
        model=self.model,
        max_tokens=kwargs.get('max_tokens', 1024),
        messages=[{"role": "user", "content": prompt}]
    ) as stream:
        for text in stream.text_stream:
            yield text

class LLMFactory: @staticmethod def create(provider: str, **kwargs) -> LLMProvider: providers = { 'claude': ClaudeProvider, 'gpt': GPTProvider, 'gemini': GeminiProvider, 'ollama': OllamaProvider } return providersprovider ```

Prompt Engineering Best Practices

```python

Structured prompts

SYSTEM_PROMPT = """You are a helpful coding assistant.

Guidelines:

  1. Write clean, well-documented code
  2. Follow best practices
  3. Explain your reasoning """

Few-shot examples

FEW_SHOT_PROMPT = """Convert natural language to SQL.

Example 1: Input: Get all users Output: SELECT * FROM users;

Example 2: Input: Count active orders Output: SELECT COUNT(*) FROM orders WHERE status = 'active';

Input: {user_input} Output:"""

Chain of thought

COT_PROMPT = """Solve this step by step: {problem}

Let's think through this: 1.""" ```

Extended Thinking Integration

Extended thinking enables Claude models to "think" before responding, improving accuracy on complex tasks like coding, math, and scientific reasoning.

When to Use Extended Thinking

  • Complex multi-step reasoning tasks
  • Code architecture and system design
  • Mathematical problem-solving
  • Scientific analysis and research
  • Strategic planning and decision-making

Cross-reference: See .claude/skills/extended-thinking/SKILL.md for detailed guidance.

Basic Extended Thinking

```python import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

Adaptive thinking (current models: Opus 4.6+, Sonnet 5, Fable 5)

Note: budget_tokens is removed on Opus 4.7+/Sonnet 5/Fable 5 (returns 400);

use output_config effort levels to control depth

response = client.messages.create( model="claude-sonnet-5", max_tokens=16000, thinking={"type": "adaptive"}, output_config={"effort": "high"}, messages=[ { "role": "user", "content": "Design a scalable microservices architecture for a multi-tenant SaaS platform" } ] )

Response includes thinking and final text

for block in response.content: if block.type == "thinking": print(f"Thinking: {block.thinking}") elif block.type == "text": print(f"Response: {block.text}") ```

Extended Thinking with Streaming

```python

Stream thinking process in real-time

with client.messages.stream( model="claude-sonnet-5", max_tokens=16000, thinking={"type": "adaptive"}, messages=[ {"role": "user", "content": "Analyze the time complexity of this sorting algorithm..."} ] ) as stream: for event in stream: if event.type == "content_block_start": if event.content_block.type == "thinking": print("\n[Thinking Process]") elif event.content_block.type == "text": print("\n[Final Answer]") elif event.type == "content_block_delta": if event.delta.type == "thinking_delta": print(event.delta.thinking, end="", flush=True) elif event.delta.type == "text_delta": print(event.delta.text, end="", flush=True) ```

Multi-Provider Extended Thinking Abstraction

```python from typing import Optional, Dict, Any from dataclasses import dataclass

@dataclass class ThinkingConfig: enabled: bool = False effort: str = "high" # low | medium | high | xhigh | max show_thinking: bool = True

class ExtendedThinkingProvider: """Abstract extended thinking across providers"""

def __init__(self, provider: str, config: ThinkingConfig):
    self.provider = provider
    self.config = config

def generate_with_thinking(self, prompt: str, **kwargs) -> Dict[str, Any]:
    if self.provider == "claude":
        return self._claude_thinking(prompt, **kwargs)
    elif self.provider == "gpt":
        # Simulate thinking with chain-of-thought
        return self._gpt_cot_thinking(prompt, **kwargs)
    else:
        raise ValueError(f"Provider {self.provider} doesn't support extended thinking")

def _claude_thinking(self, prompt: str, **kwargs) -> Dict[str, Any]:
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

    thinking_params = {}
    if self.config.enabled:
        thinking_params["thinking"] = {"type": "adaptive"}
        thinking_params["output_config"] = {"effort": self.config.effort}

    response = client.messages.create(
        model=kwargs.get("model", "claude-sonnet-5"),
        max_tokens=kwargs.get("max_tokens", 16000),
        messages=[{"role": "user", "content": prompt}],
        **thinking_params
    )

    result = {"thinking": None, "response": None}
    for block in response.content:
        if block.type == "thinking":
            result["thinking"] = block.thinking
        elif block.type == "text":
            result["response"] = block.text

    return result

def _gpt_cot_thinking(self, prompt: str, **kwargs) -> Dict[str, Any]:
    """Use chain-of-thought prompting for GPT models"""
    from openai import OpenAI
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

    cot_prompt = f"""Let's approach this step-by-step:

{prompt}

First, think through the problem systematically, then provide your final answer."""

    response = client.chat.completions.create(
        model=kwargs.get("model", "gpt-4"),
        messages=[{"role": "user", "content": cot_prompt}]
    )

    # Parse thinking from response (heuristic-based)
    content = response.choices[0].message.content
    parts = content.split("\n\n")

    return {
        "thinking": "\n\n".join(parts[:-1]) if len(parts) > 1 else None,
        "response": parts[-1] if parts else content
    }

Usage

thinking_provider = ExtendedThinkingProvider( provider="claude", config=ThinkingConfig(enabled=True, effort="high") )

result = thinking_provider.generate_with_thinking( "Design a distributed caching strategy for a multi-tenant system" )

if result["thinking"]: print(f"Thinking Process:\n{result['thinking']}\n") print(f"Final Answer:\n{result['response']}") ```

Claude Prompt Engineering Best Practices

Based on Anthropic's official guidelines for optimal performance.

1. Clear and Direct Instructions

```python

BAD: Vague request

prompt = "Make this code better"

GOOD: Specific instructions

prompt = """Refactor this Python function to:

  1. Use type hints
  2. Add comprehensive docstrings
  3. Handle edge cases (empty input, None values)
  4. Improve variable naming for clarity
  5. Add input validation

Code: {code} """ ```

2. Use XML Tags for Structure

```python

XML tags help Claude parse complex inputs

prompt = """Analyze this codebase and identify security vulnerabilities.

Provide output in this format: High|Medium|Low path/to/file.py 123 Clear description How to fix """ ```

3. Provide Examples (Few-Shot Prompting)

```python FEW_SHOT_TEMPLATE = """Convert user stories to acceptance criteria.

Now convert this user story: <user_story> {user_input} </user_story> """ ```

4. Assign Roles for Context

```python ROLE_BASED_SYSTEM_PROMPTS = { "code_reviewer": """You are an expert code reviewer with 15 years of experience. Your expertise includes:

  • Software architecture and design patterns
  • Security best practices (OWASP Top 10)
  • Performance optimization
  • Code maintainability and readability

When reviewing code:

  1. Identify bugs and potential issues
  2. Suggest improvements for clarity and performance
  3. Check for security vulnerabilities
  4. Recommend design pattern improvements
  5. Ensure code follows language best practices

Be constructive and specific in your feedback.""",

"architect": """You are a senior software architect specializing in:
  • Microservices and distributed systems
  • Cloud-native architecture (AWS, GCP, Azure)
  • Database design and optimization
  • API design (REST, GraphQL, gRPC)
  • Security and compliance

When designing systems:

  1. Consider scalability and performance

  2. Ensure fault tolerance and resilience

  3. Design for observability (logging, metrics, tracing)

  4. Follow cloud-native best practices

  5. Consider cost optimization""",

    "security_expert": """You are a security specialist focused on:

  • OWASP Top 10 vulnerabilities
  • Authentication and authorization (OAuth, OIDC, JWT)
  • Data encryption and privacy
  • Secure coding practices
  • Compliance (GDPR, HIPAA, SOC2)

When analyzing security:

  1. Identify vulnerabilities with severity ratings
  2. Provide specific remediation steps
  3. Reference security standards and best practices
  4. Consider both code-level and architectural security""" }

Usage

message = client.messages.create( model="claude-sonnet-5", max_tokens=4096, system=ROLE_BASED_SYSTEM_PROMPTS["code_reviewer"], messages=[ {"role": "user", "content": f"Review this code:\n\n{code}"} ] ) ```

5. Chain of Thought Prompting

```python COT_PROMPT = """Solve this problem step by step, showing your reasoning at each stage.

Problem: {problem}

Think through this by:

  1. Understanding what's being asked
  2. Identifying relevant information
  3. Breaking down the problem into steps
  4. Solving each step
  5. Verifying the solution

Show your work for each step."""

For complex reasoning, combine with extended thinking

response = client.messages.create( model="claude-sonnet-5", max_tokens=16000, thinking={"type": "adaptive"}, messages=[ {"role": "user", "content": COT_PROMPT.format(problem=complex_problem)} ] ) ```

6. Structured Outputs for Format Control

```python

NOTE: assistant-turn prefills return a 400 on current models

(Opus 4.6+, Sonnet 5, Fable 5). Use structured outputs instead:

response = client.messages.create( model="claude-sonnet-5", max_tokens=1024, output_config={ "format": { "type": "json_schema", "schema": { "type": "object", "properties": { "entities": {"type": "array", "items": {"type": "string"}} }, "required": ["entities"], "additionalProperties": False } } }, messages=[ {"role": "user", "content": "Extract entities from: 'Apple Inc. hired John Smith as CEO in 2023.'"} ] )

Output is guaranteed to match the schema

json_output = response.content[0].text ```

7. Long Context Best Practices

```python

For Claude's 1M token context window (200k on Haiku 4.5)

LONG_CONTEXT_TEMPLATE = """I'm providing a large codebase for analysis. The most important files for this task are at the END of this message.

<critical_files> {important_files} </critical_files>

Focus primarily on the critical files when answering the task.""" ```

Token Budget Management

Anthropic Token Counting

```python import anthropic

def count_tokens_anthropic(text: str, model: str = "claude-sonnet-5") -> int: """Count tokens using Anthropic's API""" client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

# Use count_tokens endpoint
result = client.messages.count_tokens(
    model=model,
    messages=[{"role": "user", "content": text}]
)

return result.input_tokens

def count_tokens_with_system(messages: list, system: str = None, model: str = "claude-sonnet-5") -> dict: """Count tokens including system prompt and messages""" client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

params = {
    "model": model,
    "messages": messages
}
if system:
    params["system"] = system

result = client.messages.count_tokens(**params)

return {
    "input_tokens": result.input_tokens,
    "system_tokens": getattr(result, "system_tokens", 0)
}

```

Smart Token Budget Management

```python from typing import List, Dict import anthropic

class TokenBudgetManager: """Intelligent token budget management for LLM calls"""

def __init__(self, model: str = "claude-sonnet-5"):
    self.model = model
    self.client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

    # Model-specific limits
    self.limits = {
        "claude-fable-5": {"context": 1000000, "output": 128000},
        "claude-opus-4-8": {"context": 1000000, "output": 128000},
        "claude-sonnet-5": {"context": 1000000, "output": 128000},
        "claude-haiku-4-5": {"context": 200000, "output": 64000},
    }

def get_budget(self, model: str = None) -> Dict[str, int]:
    """Get token limits for model"""
    model = model or self.model
    return self.limits.get(model, {"context": 200000, "output": 64000})

def check_budget(
    self,
    messages: List[Dict],
    system: str = None,
    max_tokens: int = 4096,
    thinking_tokens: int = 0
) -> Dict[str, any]:
    """Check if request fits within budget"""

    # Count input tokens
    token_count = self.client.messages.count_tokens(
        model=self.model,
        messages=messages,
        system=system
    )

    input_tokens = token_count.input_tokens
    budget = self.get_budget()

    # Calculate total required tokens
    total_required = input_tokens + max_tokens + thinking_tokens

    return {
        "fits_budget": total_required <= budget["context"],
        "input_tokens": input_tokens,
        "requested_output": max_tokens,
        "thinking_budget": thinking_tokens,
        "total_required": total_required,
        "context_limit": budget["context"],
        "remaining": budget["context"] - total_required,
        "utilization_pct": (total_required / budget["context"]) * 100
    }

def optimize_for_budget(
    self,
    messages: List[Dict],
    system: str = None,
    target_output: int = 4096,
    thinking_tokens: int = 0,
    priority_last_n: int = 3
) -> List[Dict]:
    """Truncate messages to fit budget, preserving recent context"""

    budget = self.get_budget()
    available = budget["context"] - target_output - thinking_tokens

    # Always keep system prompt and last N messages
    preserved_messages = messages[-priority_last_n:]

    # Count tokens for preserved content
    preserved_count = self.client.messages.count_tokens(
        model=self.model,
        messages=preserved_messages,
        system=system
    ).input_tokens

    if preserved_count <= available:
        # Try to include earlier messages
        remaining = available - preserved_count
        earlier_messages = messages[:-priority_last_n]

        # Binary search to find how many earlier messages fit
        left, right = 0, len(earlier_messages)
        best_fit = 0

        while left <= right:
            mid = (left + right) // 2
            test_messages = earlier_messages[-mid:] + preserved_messages

            test_count = self.client.messages.count_tokens(
                model=self.model,
                messages=test_messages,
                system=system
            ).input_tokens

            if test_count <= available:
                best_fit = mid
                left = mid + 1
            else:
                right = mid - 1

        return earlier_messages[-best_fit:] + preserved_messages if best_fit > 0 else preserved_messages
    else:
        # Even preserved messages exceed budget, truncate them
        return preserved_messages[-1:]  # Keep at least the last message

def get_recommendations(self, budget_check: Dict) -> List[str]:
    """Get recommendations based on budget utilization"""
    recommendations = []

    util = budget_check["utilization_pct"]

    if util > 90:
        recommendations.append("CRITICAL: Token usage >90%. Consider reducing context or output length.")
        recommendations.append("Enable extended thinking only if necessary for task complexity.")
    elif util > 75:
        recommendations.append("WARNING: Token usage >75%. Monitor context size.")
        recommendations.append("Consider summarizing earlier conversation turns.")
    elif util > 50:
        recommendations.append("Moderate token usage. Budget healthy.")
    else:
        recommendations.append("Low token usage. Budget has plenty of headroom.")

    if budget_check["thinking_budget"] > budget_check["requested_output"]:
        recommendations.append("Thinking budget exceeds output budget. Ensure this is intentional.")

    return recommendations

Usage example

manager = TokenBudgetManager(model="claude-sonnet-5")

messages = [ {"role": "user", "content": "What is Python?"}, {"role": "assistant", "content": "Python is a high-level programming language..."}, {"role": "user", "content": "Write a complex microservices architecture"} ]

system = "You are an expert software architect."

Shortened here. Read the whole file on GitHub.

Signals

GitHub stars
21
Forks
2
Last commit
Sep 2026
Advanced
Catalog kind
skill
Gateway key
llmintegration
Source
github.com/thelobbi/claude