Documentation

Everything you need to integrate LLMShield into your application.

Quick Start

LLMShield exposes an OpenAI-compatible endpoint. Point any existing OpenAI client at your gateway URL and you're done.

curl
curl -X POST https://your-gateway.onrender.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      { "role": "user", "content": "What is LLMShield?" }
    ]
  }'

That's it. Your request is now automatically cached, guarded against PII, routed to the optimal model, and logged with cost data.

SDK Integration

The LLMShield SDK provides a lightweight wrapper around the gateway API, giving you a clean interface to integrate LLMShield into any Node.js application.

Note: The SDK package will be published to npm soon. In the meantime, you can use npm link to integrate it locally into your projects.

Local Setup via npm link

terminal
# From the LLMShield project root, link the SDK globally
cd sdk
npm link

# Then, in your application directory
cd /path/to/your-app
npm link llmshield-sdk

Simple usage

app.js
const { createLLMClient } = require('llmshield-sdk');

const client = createLLMClient({
  baseURL: 'https://your-gateway.onrender.com'
});

// Simple: just pass a prompt string
const answer = await client.chat('What is machine learning?');
console.log(answer);

Advanced usage (full OpenAI body)

advanced.js
// Use chatRaw() for full control over the request body
const response = await client.chatRaw({
  messages: [
    { role: 'system', content: 'You are a helpful coding assistant.' },
    { role: 'user', content: 'Write a binary search in Python' }
  ]
});

console.log(response.choices[0].message.content);
console.log(response._llmshield);
// → { model: 'llama-3.3-70b-versatile', cost: 0.002, latency: 1200, cacheHit: false }

Error handling

error-handling.js
try {
  const answer = await client.chat('My email is john@test.com');
} catch (err) {
  if (err.status === 403) {
    console.log('Blocked by guardrail:', err.details.message);
    // → "Email address detected"
  }
}

API Reference

POST/v1/chat/completions

Send a chat completion request through the gateway.

Request Body

FieldTypeDescription
messagesarrayArray of message objects with role and content
modelstring?Optional. Overrides smart routing to use a specific model.

Success Response

200 OK
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "model": "llama-3.1-8b-instant",
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "Machine learning is a subset of AI..."
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 43,
    "completion_tokens": 30,
    "total_tokens": 73
  },
  "_llmshield": {
    "model": "llama-3.1-8b-instant",
    "cost": 0.0000046,
    "latency": 407,
    "cacheHit": false
  }
}

The _llmshield block is added by the gateway with per-request metadata.

Cached Response

200 OK (cache hit)
{
  "id": "cache-1775267576857",
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "Machine learning is a subset of AI..."
    }
  }],
  "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0 },
  "_llmshield": {
    "cacheHit": true,
    "latency": 1
  }
}

Guardrails

LLMShield scans every incoming prompt for sensitive data patterns before forwarding to the LLM. Blocked requests never reach the provider.

Email Addresses
john@example.com
Phone Numbers
555-867-5309
Credit Cards
4532-1234-5678-9012
SSN / Aadhaar
123-45-6789

Blocked Response

403 Forbidden
{
  "error": "Request blocked by guardrail",
  "details": {
    "blocked": true,
    "type": "email",
    "message": "Email address detected"
  }
}

Smart Routing

The gateway automatically selects the best model based on prompt complexity.

ConditionModelWhy
Short / simple promptsllama-3.1-8b-instantFast, cheap, good for factual Q&A
Long / complex promptsllama-3.3-70b-versatilePowerful, better for code gen and reasoning
Explicit model fieldAs specifiedOverride routing entirely

Caching

The gateway normalizes each prompt (lowercase, trim whitespace) and hashes it with SHA-256. If the hash exists in Redis, the cached response is returned instantly at zero cost and sub-millisecond latency.

Cache Miss
~400-1200ms
Full round trip to LLM provider
Cache Hit
<1ms
Zero tokens, zero cost

Configuration

Set these environment variables in the gateway's .env file.

VariableDefaultDescription
GROQ_API_KEY—Your Groq API key
DATABASE_URLpostgresql://...localhost:5432PostgreSQL connection string
REDIS_URLredis://localhost:6379Redis connection string
SMALL_MODELllama-3.1-8b-instantModel for short prompts
LARGE_MODELllama-3.3-70b-versatileModel for long prompts
EMBEDDINGS_ENABLEDfalseFuture: vector cache toggle