Back to guides
4
6 min

AI Gateway

Brand Guardrails & Routing

Why Marketing AI Needs a Gateway

Every production AI system needs a control plane between the user and the LLM. For marketing intelligence, the stakes are uniquely high:

  • Brand voice compliance — a content draft that sounds "off-brand" is worse than no draft at all
  • Confidential data gating — internal campaign budgets, competitive intelligence sources, and pricing strategy must never appear in external-facing content
  • Competitor mention policies — many brands have strict rules about naming competitors in public content
  • Cost control — marketing teams run dozens of analyses per day; without budgets, costs spiral
  • The AI Gateway handles all of this as a LangGraph.js state graph — a directed graph where each step is a node, transitions are conditional edges, and the entire execution is traceable.

    LangGraph Architecture

    Why a Graph, Not If/Else

    Traditional approaches use nested conditional logic:

    if (isCached(query)) return cache.get(query);
    if (hasConfidentialData(query)) return reject();
    if (isContentDraft(query)) model = "opus";
    // ... more nesting

    This becomes unmaintainable as you add guardrails, caching, routing, and tracking. LangGraph replaces it with a visible graph:

    classify → cache_check → [hit] → return_cached
                           → [miss] → guardrails → route → llm → brand_check → format → track

    Each node is a pure function. Edges are conditional transitions. You can visualize the graph, trace any request through it, and add new nodes without touching existing ones.

    State Object

    The GatewayState type flows through every node:

    interface GatewayState {
      query: string;
      intent: "competitive" | "trend" | "performance" | "content_draft" | "strategic";
      complexity: "simple" | "moderate" | "complex";
      sensitivity: "normal" | "confidential" | "restricted";
      retrievedContext: string;
      llmResponse: string;
      brandCompliance: { score: number; violations: string[] };
      model: string;
      cached: boolean;
      tokensUsed: number;
      latencyMs: number;
    }

    Each node reads what it needs from state and writes its outputs back. The classify node writes intent, complexity, and sensitivity. The route node reads those to write model. The brand check node reads llmResponse and writes brandCompliance.

    The Nodes

    Classify

    Analyzes the incoming query to determine intent, complexity, and sensitivity. For marketing queries:

  • "What is CompetitorX doing?" → competitive, moderate, normal
  • "Draft a LinkedIn post about our Q4 results" → content_draft, complex, normal
  • "What's our campaign budget vs CompetitorX's estimated spend?" → competitive, moderate, confidential
  • "How has market share shifted?" → trend, complex, normal
  • Classification drives every downstream decision — which model to use, which guardrails to apply, how to format the output.

    Cache Check

    Semantic caching: embed the query and check if a similar query (cosine similarity > 0.95) was answered recently. Cache TTLs vary by query type:

    Query TypeTTLWhy
    Performance data1 hourMetrics change frequently
    Competitive analysis6 hoursPositioning changes slowly
    Industry trends24 hoursStable over days
    Content draftsNo cacheEach draft should be unique

    Cache hits save 80-90% of the cost and return instantly. For a marketing team running the same competitor analysis multiple times a day, this is critical.

    Guardrails

    This node runs a series of checks. If any check fails, the query is either sanitized or rejected.

    Brand Voice Compliance:

    Load the brand guidelines from the data lake. Check that any content in the query or response uses approved terminology, maintains the right tone, and doesn't contradict messaging pillars.

    Example: If brand guidelines say "We never use fear-based messaging," flag any query or response containing scare tactics.

    Confidential Data Gate:

    Detect and block queries that would expose internal data in external contexts. Pattern matching for:

  • Budget numbers with dollar signs in "draft for external" context
  • Competitive intelligence source names (analyst firms, scraping tools)
  • Internal performance targets and OKRs
  • Unpublished product roadmap details
  • Competitor Mention Sanitizer:

    Many brands have policies about naming competitors in public content. The sanitizer:

  • Detects competitor names in content-drafting queries
  • Replaces them with approved alternatives ("legacy approaches," "traditional solutions")
  • Logs the sanitization for audit
  • Route

    Select the right model based on classification:

  • Simple lookups (metric retrieval, status checks) → fast, cheap model (Haiku class)
  • Competitive analysis, trend reports → capable model (Sonnet class)
  • Content drafting with brand voice → most capable model (Opus class) for nuanced tone
  • Confidential queries → reject with explanation, log the attempt
  • LLM Call

    Invoke the selected model with the retrieved context from Module 3. Key patterns:

  • Retry with exponential backoff (3 attempts, 1s/2s/4s delays)
  • Timeout after 30 seconds
  • Fallback chain: primary model → secondary model → graceful error
  • Stream responses for real-time feedback in the UI
  • Brand Check (Post-LLM)

    After the LLM generates a response, re-validate against brand guidelines:

  • Does the response use approved terminology?
  • Does it maintain the right tone for the intended audience?
  • Does it include proper disclaimers for competitive claims?
  • Does it contradict any messaging pillars?
  • The brand check returns a compliance score (0-100) and a list of violations. If the score is below threshold, the response is sent back through the LLM with specific fix instructions.

    Format

    Structure the final output with:

  • Source citations with time periods
  • Confidence score based on retrieval quality and brand compliance
  • Metadata: model used, tokens consumed, cache status, execution time
  • Usage Tracking

    Log every request:

  • Tokens consumed (prompt + completion)
  • Estimated cost
  • Latency (per-node and total)
  • Model used
  • Cache hit/miss
  • Brand compliance score
  • Check per-team daily budgets. Alert at 80% and reject at 100%.

    Cost Controls

    Marketing teams can run hundreds of queries per day. Without cost controls, a single enthusiastic analyst can burn through a month's AI budget in a week.

    The gateway enforces:

  • Per-team daily token budget — configurable per team (e.g., content team: 500K tokens/day, strategy team: 1M tokens/day)
  • Budget alerts — Slack notification at 80% usage
  • Hard limit — graceful rejection at 100% with "budget exhausted" message and reset time
  • Model-specific pricing — track actual cost per model, not just tokens
  • What You'll Build

  • Install LangGraph.js and define the GatewayState type
  • Build the classify and route nodes with marketing-specific intent categories
  • Implement brand voice guardrails, confidential data gating, and competitor sanitization
  • Add semantic caching with type-specific TTLs
  • Build usage tracking with per-team budgets
  • Run the full graph end-to-end with different query types
  • Glossary

    TermMeaning
    State graphA directed graph where nodes are functions and edges are transitions
    LangGraphA library for building stateful, multi-step AI workflows as graphs
    GuardrailA check that validates, sanitizes, or rejects AI inputs/outputs
    Brand complianceEnsuring AI-generated content matches brand voice, tone, and terminology
    Confidential gateA check that prevents internal data from appearing in external-facing content
    Semantic cacheCaching responses by query similarity rather than exact string match

    This is chapter 4 of AI Marketing Intelligence.

    Get the full hands-on course for $100 and build the complete system. Your projects become your portfolio.

    View course details