Menu

Change language

GitHub repo

Model Token Cost Optimizer

blockai

Helicone and Portkey style AI gateway telemetry, semantic caching hit rates, model routing savings, and latency metrics dashboard with token spend analytics, model allocation breakdown, and active cost optimization rules.

Installation

$npx uipkge-ng@latest add model-token-cost-optimizer
Manifest:https://uipkge.dev/r/angular/model-token-cost-optimizer.jsonInstalls to:src/app/components/blocks/model-token-cost-optimizer/

Variants

Loading interactive previews…

Schema

Type aliases exported from this item's source. Use these to shape the data you pass in.

ModelAllocation
interface ModelAllocation {
  id: string
  name: string
  provider: string
  providerTag: string
  sharePercent: number
  inputTokens: string
  outputTokens: string
  totalTokens: string
  totalCost: string
  reroutesHandled: number
  avgLatency: string
  role: string
  status: 'optimal' | 'active'
}
CacheSegment
interface CacheSegment {
  name: string
  type: CacheSegmentType
  share: number
  requests: string
  tokensSpared: string
  costSaved: string
  latency: string
  color: string
  badgeVariant: Extract<BadgeVariant, 'success' | 'info' | 'outline'>
  description: string
}

Files installed (2)

  • src/app/components/blocks/model-token-cost-optimizer/model-token-cost-optimizer.component.ts36.5 kB
    import { Component, Input, computed, signal, ChangeDetectionStrategy } from '@angular/core'
    import {
      ArrowRightLeft,
      BrainCircuit,
      CheckCircle2,
      Coins,
      Download,
      Gauge,
      Layers,
      LucideAngularModule,
      Route,
      SlidersHorizontal,
      Sparkles,
      TrendingDown,
      Zap,
    } from 'lucide-angular'
    import { cn } from '@/lib/utils'
    import { UiBadgeComponent, type BadgeVariant } from '@/ui/badge/badge.component'
    import { UiButtonComponent } from '@/ui/button/button.component'
    import {
      UiCardComponent,
      UiCardContentComponent,
      UiCardDescriptionComponent,
      UiCardHeaderComponent,
      UiCardTitleComponent,
    } from '@/ui/card/card.component'
    import { UiProgressComponent } from '@/ui/progress/progress.component'
    import {
      UiSelectComponent,
      UiSelectContentComponent,
      UiSelectItemComponent,
      UiSelectTriggerComponent,
      UiSelectValueComponent,
    } from '@/ui/select/select.component'
    import { UiSwitchComponent } from '@/ui/switch/switch.component'
    import {
      UiTableBodyComponent,
      UiTableCellComponent,
      UiTableComponent,
      UiTableHeadComponent,
      UiTableHeaderComponent,
      UiTableRowComponent,
    } from '@/ui/table/table.component'
    
    export interface ModelAllocation {
      id: string
      name: string
      provider: string
      providerTag: string
      sharePercent: number
      inputTokens: string
      outputTokens: string
      totalTokens: string
      totalCost: string
      reroutesHandled: number
      avgLatency: string
      role: string
      status: 'optimal' | 'active'
    }
    
    export type CacheSegmentType = 'exact' | 'semantic' | 'origin'
    
    export interface CacheSegment {
      name: string
      type: CacheSegmentType
      share: number
      requests: string
      tokensSpared: string
      costSaved: string
      latency: string
      color: string
      badgeVariant: Extract<BadgeVariant, 'success' | 'info' | 'outline'>
      description: string
    }
    
    const models: ModelAllocation[] = [
      {
        id: 'claude-3-5-sonnet',
        name: 'Claude 3.5 Sonnet',
        provider: 'Anthropic',
        providerTag: 'Anthropic Direct',
        sharePercent: 45,
        inputTokens: '24.2M',
        outputTokens: '4.1M',
        totalTokens: '28.3M',
        totalCost: '$1,824.20',
        reroutesHandled: 142,
        avgLatency: '680ms',
        role: 'Complex Reasoning & System Arch',
        status: 'optimal',
      },
      {
        id: 'gpt-4o',
        name: 'GPT-4o',
        provider: 'OpenAI',
        providerTag: 'OpenAI Tier-5',
        sharePercent: 30,
        inputTokens: '12.5M',
        outputTokens: '2.6M',
        totalTokens: '15.1M',
        totalCost: '$1,080.50',
        reroutesHandled: 64,
        avgLatency: '540ms',
        role: 'Multi-Modal & Structured Schema',
        status: 'active',
      },
      {
        id: 'llama-3-3-70b',
        name: 'Llama 3.3 70B',
        provider: 'Groq',
        providerTag: 'Groq LPU Inference',
        sharePercent: 15,
        inputTokens: '4.1M',
        outputTokens: '1.2M',
        totalTokens: '5.3M',
        totalCost: '$195.40',
        reroutesHandled: 8,
        avgLatency: '190ms',
        role: 'High-Throughput Transformation',
        status: 'optimal',
      },
      {
        id: 'gpt-4o-mini',
        name: 'GPT-4o-mini',
        provider: 'OpenAI',
        providerTag: 'OpenAI Direct',
        sharePercent: 10,
        inputTokens: '2.0M',
        outputTokens: '0.5M',
        totalTokens: '2.5M',
        totalCost: '$320.40',
        reroutesHandled: 12,
        avgLatency: '240ms',
        role: 'Intent Classification & Tagging',
        status: 'active',
      },
    ]
    
    const cacheSegments: CacheSegment[] = [
      {
        name: 'Exact Cache',
        type: 'exact',
        share: 24.0,
        requests: '184.2K reqs (24.0%)',
        tokensSpared: '18.2M tokens',
        costSaved: '$2,136.00 saved',
        latency: '12ms p50',
        color: 'bg-success',
        badgeVariant: 'success',
        description: 'Deterministic SHA-256 hit on normalized system prompt and user parameters.',
      },
      {
        name: 'Semantic Vector Cache',
        type: 'semantic',
        share: 18.8,
        requests: '144.3K reqs (18.8%)',
        tokensSpared: '14.3M tokens',
        costSaved: '$1,676.00 saved',
        latency: '28ms p50',
        color: 'bg-info',
        badgeVariant: 'info',
        description: 'Vector similarity cache match with cosine threshold ≥ 0.88 via Qdrant/Redis.',
      },
      {
        name: 'Origin Model Calls',
        type: 'origin',
        share: 57.2,
        requests: '439.0K reqs (57.2%)',
        tokensSpared: '0 (billed)',
        costSaved: '$3,420.50 spent',
        latency: '840ms p99',
        color: 'bg-muted-foreground/30 dark:bg-muted-foreground/20',
        badgeVariant: 'outline',
        description: 'Forwarded to upstream LLMs via smart model routing with fallback circuit protection.',
      },
    ]
    
    const segmentLatency: Record<CacheSegmentType, string> = {
      exact: '12ms p50',
      semantic: '28ms p50',
      origin: '840ms p99',
    }
    
    const providerLabels: Record<string, string> = {
      Anthropic: 'Anthropic',
      OpenAI: 'OpenAI',
      Groq: 'Groq',
    }
    
    /**
     * AI gateway telemetry dashboard: token spend KPIs, a semantic-cache savings visualizer,
     * the model routing / cost allocation table (filterable by provider) and the active
     * cost-optimization rules with their toggles. Angular port of the React
     * `ModelTokenCostOptimizer` block (same markup, classes, copy and sample data).
     */
    @Component({
      changeDetection: ChangeDetectionStrategy.Eager,
      selector: 'ui-model-token-cost-optimizer, [ui-model-token-cost-optimizer]',
      standalone: true,
      imports: [
        LucideAngularModule,
        UiBadgeComponent,
        UiButtonComponent,
        UiCardComponent,
        UiCardContentComponent,
        UiCardDescriptionComponent,
        UiCardHeaderComponent,
        UiCardTitleComponent,
        UiProgressComponent,
        UiSelectComponent,
        UiSelectContentComponent,
        UiSelectItemComponent,
        UiSelectTriggerComponent,
        UiSelectValueComponent,
        UiSwitchComponent,
        UiTableBodyComponent,
        UiTableCellComponent,
        UiTableComponent,
        UiTableHeadComponent,
        UiTableHeaderComponent,
        UiTableRowComponent,
      ],
      host: {
        '[attr.data-slot]': '"model-token-cost-optimizer"',
        '[class]': 'hostClass',
      },
      template: `
        <!-- Header Section -->
        <div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
          <div class="space-y-1">
            <div class="flex flex-wrap items-center gap-2.5">
              <h1 class="text-foreground text-2xl font-bold tracking-tight sm:text-3xl">
                AI Gateway &amp; Token Cost Optimizer
              </h1>
              <span
                ui-badge
                wrap
                variant="outline"
                class="border-success/30 bg-success/10 text-success gap-1.5 text-xs font-medium"
              >
                <span class="bg-success size-1.5 animate-pulse rounded-full" aria-hidden="true"></span>
                <span>Routing Active · 99.99% Availability</span>
              </span>
            </div>
            <p class="text-muted-foreground text-sm">
              Helicone &amp; Portkey style gateway telemetry, semantic caching hit rates, model routing savings, and latency
              metrics.
            </p>
          </div>
    
          <div class="flex flex-wrap items-center gap-2.5">
            <ui-select [value]="timeframe()" (valueChange)="timeframe.set($event)">
              <button ui-select-trigger class="w-full text-xs font-medium sm:w-52" aria-label="Select timeframe">
                <ui-select-value placeholder="Select timeframe" />
              </button>
              <ui-select-content>
                <ui-select-item value="aug-2026">This Month · August 2026</ui-select-item>
                <ui-select-item value="jul-2026">Last Month · July 2026</ui-select-item>
                <ui-select-item value="q2-2026">Q2 2026 Summary</ui-select-item>
                <ui-select-item value="ytd-2026">Year to Date 2026</ui-select-item>
              </ui-select-content>
            </ui-select>
    
            <!-- px-2.5: React's size="sm" gets it from has-[>svg], but here the svg sits inside <lucide-icon>. -->
            <button ui-button aria-label="Download attachment" variant="outline" size="sm" class="gap-1.5 px-2.5 text-xs">
              <lucide-icon [img]="Download" class="text-muted-foreground size-3.5" />
              <span>Export Token Logs</span>
            </button>
    
            <button ui-button size="sm" class="gap-1.5 px-2.5 text-xs">
              <lucide-icon [img]="SlidersHorizontal" class="size-3.5" />
              <span>Configure Routing Rules</span>
            </button>
          </div>
        </div>
    
        <!-- 4 Gateway Performance KPI Cards -->
        <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
          <!-- KPI 1: Total Token Spend -->
          <ui-card class="flex flex-col justify-between">
            <ui-card-header class="pb-2">
              <div class="flex flex-wrap items-center justify-between gap-2">
                <div class="flex min-w-0 items-center gap-2">
                  <div
                    aria-hidden="true"
                    class="bg-muted text-muted-foreground border-border flex size-8 shrink-0 items-center justify-center rounded-lg border shadow-xs"
                  >
                    <lucide-icon [img]="Coins" class="size-4" />
                  </div>
                  <ui-card-title class="text-sm font-medium">Total Token Spend</ui-card-title>
                </div>
                <span ui-badge wrap variant="outline" class="text-xs font-normal tabular-nums">
                  vs $8,900.00 unoptimized
                </span>
              </div>
            </ui-card-header>
            <ui-card-content class="space-y-2">
              <div class="flex flex-wrap items-baseline justify-between gap-x-2 gap-y-0.5">
                <span class="text-foreground text-2xl font-bold tracking-tight tabular-nums">$3,420.50</span>
                <span class="text-success text-success text-xs font-semibold tabular-nums">-$5,479.50 (61.5%)</span>
              </div>
              <div class="space-y-1">
                <div class="flex items-center justify-between text-xs">
                  <span class="text-muted-foreground">Budget Utilization</span>
                  <span class="text-foreground font-medium tabular-nums">38.4% of baseline</span>
                </div>
                <ui-progress [value]="38.4" class="h-1.5" />
              </div>
              <p class="text-muted-foreground border-border/60 border-t pt-2 text-xs tabular-nums">
                767,500 total gateway inference requests
              </p>
            </ui-card-content>
          </ui-card>
    
          <!-- KPI 2: Cost Savings from Caching & Routing -->
          <ui-card class="flex flex-col justify-between">
            <ui-card-header class="pb-2">
              <div class="flex flex-wrap items-center justify-between gap-2">
                <div class="flex min-w-0 items-center gap-2">
                  <div
                    aria-hidden="true"
                    class="border-success/20 bg-success/10 text-success text-success flex size-8 shrink-0 items-center justify-center rounded-lg border shadow-xs"
                  >
                    <lucide-icon [img]="TrendingDown" class="size-4" />
                  </div>
                  <ui-card-title class="text-sm font-medium">Cost Savings</ui-card-title>
                </div>
                <span
                  ui-badge
                  wrap
                  variant="outline"
                  class="border-success/30 bg-success/10 text-success text-xs font-semibold"
                >
                  61.5% Reduction
                </span>
              </div>
            </ui-card-header>
            <ui-card-content class="space-y-2">
              <div class="flex flex-wrap items-baseline justify-between gap-x-2 gap-y-0.5">
                <span class="text-success text-success text-2xl font-bold tracking-tight tabular-nums">
                  $5,479.50 Saved
                </span>
              </div>
              <div class="space-y-1">
                <div class="flex items-center justify-between text-xs">
                  <span class="text-muted-foreground">Semantic Cache Savings</span>
                  <span class="text-foreground font-medium tabular-nums">$3,812.00</span>
                </div>
                <div class="flex items-center justify-between text-xs">
                  <span class="text-muted-foreground">Smart Model Routing</span>
                  <span class="text-foreground font-medium tabular-nums">$1,667.50</span>
                </div>
              </div>
              <p class="text-muted-foreground border-border/60 border-t pt-2 text-xs tabular-nums">
                32.5M tokens prevented from origin billing
              </p>
            </ui-card-content>
          </ui-card>
    
          <!-- KPI 3: Semantic Cache Hit Rate -->
          <ui-card class="flex flex-col justify-between">
            <ui-card-header class="pb-2">
              <div class="flex flex-wrap items-center justify-between gap-2">
                <div class="flex min-w-0 items-center gap-2">
                  <div
                    aria-hidden="true"
                    class="border-info/20 bg-info/10 text-info text-info flex size-8 shrink-0 items-center justify-center rounded-lg border shadow-xs"
                  >
                    <lucide-icon [img]="Zap" class="size-4" />
                  </div>
                  <ui-card-title class="text-sm font-medium">Semantic Cache Hit Rate</ui-card-title>
                </div>
                <span ui-badge wrap variant="outline" class="border-info/30 bg-info/10 text-info text-xs font-semibold">
                  &lt; 15ms response
                </span>
              </div>
            </ui-card-header>
            <ui-card-content class="space-y-2">
              <div class="flex flex-wrap items-baseline justify-between gap-x-2 gap-y-0.5">
                <span class="text-foreground text-2xl font-bold tracking-tight tabular-nums">42.8%</span>
                <span class="text-muted-foreground text-xs font-medium tabular-nums">328,500 hits</span>
              </div>
              <div class="space-y-1">
                <div class="flex items-center justify-between text-xs">
                  <span class="text-muted-foreground">Exact Match vs Vector</span>
                  <span class="text-foreground font-medium tabular-nums">24.0% / 18.8%</span>
                </div>
                <ui-progress [value]="42.8" class="[&_[data-slot=progress-indicator]]:bg-info h-1.5" />
              </div>
              <p class="text-muted-foreground border-border/60 border-t pt-2 text-xs tabular-nums">
                p50 exact: 12ms · p50 vector: 28ms
              </p>
            </ui-card-content>
          </ui-card>
    
          <!-- KPI 4: P99 Gateway Latency -->
          <ui-card class="flex flex-col justify-between">
            <ui-card-header class="pb-2">
              <div class="flex flex-wrap items-center justify-between gap-2">
                <div class="flex min-w-0 items-center gap-2">
                  <div
                    aria-hidden="true"
                    class="bg-muted text-muted-foreground border-border flex size-8 shrink-0 items-center justify-center rounded-lg border shadow-xs"
                  >
                    <lucide-icon [img]="Gauge" class="size-4" />
                  </div>
                  <ui-card-title class="text-sm font-medium">P99 Gateway Latency</ui-card-title>
                </div>
                <span ui-badge wrap variant="outline" class="text-xs font-normal">4.2x Faster</span>
              </div>
            </ui-card-header>
            <ui-card-content class="space-y-2">
              <div class="flex flex-wrap items-baseline justify-between gap-x-2 gap-y-0.5">
                <span class="text-foreground text-2xl font-bold tracking-tight tabular-nums">840ms</span>
                <span class="text-muted-foreground text-xs font-medium tabular-nums">p50: 180ms</span>
              </div>
              <div class="space-y-1">
                <div class="flex items-center justify-between text-xs">
                  <span class="text-muted-foreground">Smart Fallback SLA</span>
                  <span class="text-success text-success font-medium tabular-nums">Zero 504 timeouts</span>
                </div>
                <ui-progress [value]="28" class="[&_[data-slot=progress-indicator]]:bg-success h-1.5" />
              </div>
              <p class="text-muted-foreground border-border/60 border-t pt-2 text-xs tabular-nums">
                Automatic reroute on &gt;1,500ms TTFT or 429
              </p>
            </ui-card-content>
          </ui-card>
        </div>
    
        <!-- Semantic Cache Savings Visualizer -->
        <ui-card>
          <ui-card-header>
            <div class="flex flex-col gap-1 sm:flex-row sm:items-center sm:justify-between">
              <div>
                <ui-card-title class="flex min-w-0 items-center gap-2 text-base font-semibold">
                  <lucide-icon [img]="Layers" class="text-primary size-4" />
                  <span>Semantic Cache Savings &amp; Token Bypass Visualizer</span>
                </ui-card-title>
                <ui-card-description>
                  Telemetry breakdown comparing deterministic exact hash matches, semantic vector cache hits, and origin LLM
                  invocations.
                </ui-card-description>
              </div>
              <span ui-badge wrap variant="outline" class="w-fit text-xs font-medium tabular-nums">
                767,500 Total Requests Analyzed
              </span>
            </div>
          </ui-card-header>
          <ui-card-content class="space-y-4">
            <!-- Multi-segment progress visualizer bar -->
            <div class="space-y-2">
              <div
                class="border-border/60 bg-muted/40 flex h-4 w-full overflow-hidden rounded-md border p-0.5 shadow-inner"
              >
                <div
                  class="bg-success h-full w-[24.0%] rounded-xs transition-[width] duration-500"
                  title="Exact Key Cache: 24.0%"
                ></div>
                <div class="bg-background w-0.5" aria-hidden="true"></div>
                <div
                  class="bg-info h-full w-[18.8%] rounded-xs transition-[width] duration-500"
                  title="Semantic Vector Cache: 18.8%"
                ></div>
                <div class="bg-background w-0.5" aria-hidden="true"></div>
                <div
                  class="bg-muted-foreground/30 dark:bg-muted-foreground/20 h-full w-[57.2%] rounded-xs transition-[width] duration-500"
                  title="Origin Model Calls: 57.2%"
                ></div>
              </div>
    
              <!-- Visual Legend Header -->
              <div class="text-muted-foreground flex flex-wrap items-center justify-between gap-2 text-xs">
                <div class="flex items-center gap-1.5">
                  <span class="bg-success size-2.5 rounded-full" aria-hidden="true"></span>
                  <span class="text-foreground font-medium">Exact Cache:</span>
                  <span class="tabular-nums">24.0%</span>
                </div>
                <div class="flex items-center gap-1.5">
                  <span class="bg-info size-2.5 rounded-full" aria-hidden="true"></span>
                  <span class="text-foreground font-medium">Semantic Vector Cache:</span>
                  <span class="tabular-nums">18.8%</span>
                </div>
                <div class="flex items-center gap-1.5">
                  <span class="bg-muted-foreground/40 size-2.5 rounded-full" aria-hidden="true"></span>
                  <span class="text-foreground font-medium">Origin Model Calls:</span>
                  <span class="tabular-nums">57.2%</span>
                </div>
              </div>
            </div>
    
            <!-- 3 Segment Metric Detail Cards -->
            <div class="grid grid-cols-1 gap-3 sm:grid-cols-3">
              @for (segment of cacheSegments; track segment.name) {
                <div
                  class="bg-muted/30 border-border/80 flex flex-col justify-between space-y-2.5 rounded-lg border p-3.5 shadow-xs"
                >
                  <div class="space-y-1.5">
                    <div class="flex flex-wrap items-center justify-between gap-2">
                      <div class="flex min-w-0 items-center gap-2">
                        <span [class]="cn('size-2 shrink-0 rounded-full', segment.color)" aria-hidden="true"></span>
                        <span class="text-foreground truncate text-xs font-semibold">{{ segment.name }}</span>
                      </div>
                      <span
                        ui-badge
                        wrap
                        [variant]="segment.badgeVariant"
                        class="shrink-0 text-xs font-medium tabular-nums"
                      >
                        {{ latencyLabel(segment.type) }}
                      </span>
                    </div>
                    <p class="text-muted-foreground text-xs leading-relaxed">{{ segment.description }}</p>
                  </div>
    
                  <div class="border-border/60 space-y-1 border-t pt-2 text-xs">
                    <div class="flex items-center justify-between">
                      <span class="text-muted-foreground">Volume Share</span>
                      <span class="text-foreground font-medium tabular-nums">{{ segment.requests }}</span>
                    </div>
                    <div class="flex items-center justify-between">
                      <span class="text-muted-foreground">Token Impact</span>
                      <span class="text-foreground font-medium tabular-nums">{{ segment.tokensSpared }}</span>
                    </div>
                    <div class="flex items-center justify-between">
                      <span class="text-muted-foreground">Financial Impact</span>
                      <span
                        [class]="
                          cn('font-semibold tabular-nums', segment.type === 'origin' ? 'text-foreground' : 'text-success')
                        "
                      >
                        {{ segment.costSaved }}
                      </span>
                    </div>
                  </div>
                </div>
              }
            </div>
          </ui-card-content>
        </ui-card>
    
        <!-- Model Routing & Cost Allocation Table -->
        <ui-card>
          <ui-card-header>
            <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
              <div class="space-y-0.5">
                <ui-card-title class="flex min-w-0 items-center gap-2 text-base font-semibold">
                  <lucide-icon [img]="Route" class="text-primary size-4" />
                  <span>Model Routing &amp; Cost Allocation</span>
                </ui-card-title>
                <ui-card-description>
                  Multi-provider connected models, request distribution, token volumes, total cost, and automated fallback
                  retries.
                </ui-card-description>
              </div>
    
              <div class="flex min-w-0 items-center gap-2">
                <ui-select [value]="providerFilter()" (valueChange)="providerFilter.set($event)">
                  <button ui-select-trigger class="w-full text-xs sm:w-40" aria-label="Filter by provider">
                    <ui-select-value placeholder="All Providers" />
                  </button>
                  <ui-select-content>
                    <ui-select-item value="all">All Providers</ui-select-item>
                    <ui-select-item value="anthropic">Anthropic</ui-select-item>
                    <ui-select-item value="openai">OpenAI</ui-select-item>
                    <ui-select-item value="groq">Groq</ui-select-item>
                  </ui-select-content>
                </ui-select>
              </div>
            </div>
          </ui-card-header>
          <ui-card-content class="p-0 sm:p-6 sm:pt-0">
            <div class="overflow-x-auto">
              <div ui-table>
                <thead ui-table-header>
                  <tr ui-table-row>
                    <th ui-table-head class="text-xs">Model &amp; Provider</th>
                    <th ui-table-head class="text-xs">Request Share</th>
                    <th ui-table-head class="text-xs">Tokens (Input / Output)</th>
                    <th ui-table-head class="text-right text-xs">Total Cost</th>
                    <th ui-table-head class="text-right text-xs">Fallback Retries Handled</th>
                    <th ui-table-head class="text-right text-xs">Assigned Routing Role</th>
                  </tr>
                </thead>
                <tbody ui-table-body>
                  @for (model of filteredModels(); track model.id) {
                    <tr ui-table-row>
                      <!-- Model Name & Provider -->
                      <td ui-table-cell class="py-3 whitespace-nowrap">
                        <div class="flex min-w-0 items-center gap-2.5">
                          <div
                            aria-hidden="true"
                            class="bg-muted text-foreground border-border flex size-7 shrink-0 items-center justify-center rounded-md border shadow-xs"
                          >
                            <lucide-icon [img]="BrainCircuit" class="size-3.5" />
                          </div>
                          <div class="space-y-0.5">
                            <div class="text-foreground text-xs font-semibold">{{ model.name }}</div>
                            <div class="text-muted-foreground flex items-center gap-1.5 text-xs">
                              <span>{{ providerLabel(model.provider) }}</span>
                              <span class="tabular-nums">{{ model.avgLatency }}</span>
                            </div>
                          </div>
                        </div>
                      </td>
    
                      <!-- Request Share % -->
                      <td ui-table-cell class="py-3">
                        <div class="w-32 space-y-1">
                          <div class="flex items-center justify-between text-xs">
                            <span class="text-foreground font-semibold tabular-nums">{{ model.sharePercent }}%</span>
                            <span class="text-muted-foreground text-xs">share</span>
                          </div>
                          <ui-progress [value]="model.sharePercent" class="h-1.5" />
                        </div>
                      </td>
    
                      <!-- Input & Output Tokens in tabular-nums -->
                      <td ui-table-cell class="py-3 whitespace-nowrap">
                        <div class="space-y-0.5 text-xs">
                          <div class="text-foreground font-medium tabular-nums">
                            {{ model.inputTokens }} input · {{ model.outputTokens }} output
                          </div>
                          <div class="text-muted-foreground text-xs tabular-nums">
                            {{ model.totalTokens }} combined tokens
                          </div>
                        </div>
                      </td>
    
                      <!-- Total Cost -->
                      <td ui-table-cell class="py-3 text-right whitespace-nowrap">
                        <div class="text-foreground text-xs font-bold tabular-nums">{{ model.totalCost }}</div>
                        <div class="text-muted-foreground text-xs tabular-nums">
                          avg \${{ avgPerThousand(model) }} / 1k req
                        </div>
                      </td>
    
                      <!-- Fallback & Rate-Limit Retries Handled -->
                      <td ui-table-cell class="py-3 text-right whitespace-nowrap">
                        <div class="inline-flex items-center justify-end gap-1.5 text-xs">
                          <span ui-badge wrap variant="outline" class="gap-1 text-xs font-medium tabular-nums">
                            <lucide-icon [img]="ArrowRightLeft" class="text-muted-foreground size-3" />
                            <span>{{ model.reroutesHandled }} automatic reroutes</span>
                          </span>
                        </div>
                      </td>
    
                      <!-- Assigned Routing Role -->
                      <td ui-table-cell class="py-3 text-right whitespace-nowrap">
                        <span ui-badge wrap variant="secondary" class="text-xs font-normal">{{ model.role }}</span>
                      </td>
                    </tr>
                  }
                </tbody>
              </div>
            </div>
    
            <!-- Table Summary / Footer Banner -->
            <div
              class="border-border/80 bg-muted/30 mt-4 flex flex-col gap-3 rounded-lg border p-3 text-xs sm:flex-row sm:items-center sm:justify-between"
            >
              <div class="flex min-w-0 items-center gap-2">
                <lucide-icon [img]="CheckCircle2" class="text-success size-4 shrink-0" />
                <span class="text-muted-foreground">
                  Across all 4 models:
                  <strong class="text-foreground font-semibold tabular-nums"
                    >42.8M input · 8.4M output (51.2M total)</strong
                  >
                </span>
              </div>
              <div class="flex items-center gap-4 text-xs tabular-nums">
                <span class="text-muted-foreground">
                  Total Reroutes Handled: <strong class="text-foreground font-semibold">226 incidents</strong>
                </span>
                <span class="text-muted-foreground">
                  Net Origin Billed: <strong class="text-foreground font-semibold">$3,420.50</strong>
                </span>
              </div>
            </div>
          </ui-card-content>
        </ui-card>
    
        <!-- Active Cost Optimization Rules Card -->
        <ui-card>
          <ui-card-header>
            <div class="flex flex-col gap-1 sm:flex-row sm:items-center sm:justify-between">
              <div class="space-y-0.5">
                <ui-card-title class="flex min-w-0 items-center gap-2 text-base font-semibold">
                  <lucide-icon [img]="Sparkles" class="text-primary size-4" />
                  <span>Active Cost Optimization &amp; Gateway Rules</span>
                </ui-card-title>
                <ui-card-description>
                  Autonomous gateway policies eliminating redundant token expenditure and enforcing fallback SLAs.
                </ui-card-description>
              </div>
              <span
                ui-badge
                wrap
                variant="outline"
                class="border-success/30 bg-success/10 text-success w-fit text-xs font-medium"
              >
                4 / 4 Rules Active
              </span>
            </div>
          </ui-card-header>
          <ui-card-content class="space-y-4">
            <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
              <!-- Rule 1: Prompt Compression -->
              <div class="border-border bg-card flex flex-col justify-between space-y-3 rounded-lg border p-4 shadow-xs">
                <div class="flex items-start justify-between gap-3">
                  <div class="space-y-1">
                    <div class="flex min-w-0 items-center gap-2">
                      <span class="text-foreground text-sm font-semibold"
                        >Prompt Compression &amp; System Cache Priming</span
                      >
                      <span ui-badge wrap variant="success" class="text-xs">Active</span>
                    </div>
                    <p class="text-muted-foreground text-xs leading-relaxed">
                      Strips boilerplate whitespaces, trims markdown delimiters, and leverages provider system prompt prefix
                      caching to minimize input token weights.
                    </p>
                  </div>
                  <button
                    ui-switch
                    [checked]="promptCompressionEnabled()"
                    (checkedChange)="promptCompressionEnabled.set($event)"
                    aria-label="Toggle prompt compression"
                  ></button>
                </div>
                <div
                  class="border-border/60 bg-muted/40 flex items-center justify-between rounded-md border px-3 py-1.5 text-xs"
                >
                  <span class="text-muted-foreground">Estimated Rule Savings</span>
                  <span class="text-success text-success font-semibold tabular-nums">
                    ~$680.00 / mo (18% input token reduction)
                  </span>
                </div>
              </div>
    
              <!-- Rule 2: Automatic small-model routing -->
              <div class="border-border bg-card flex flex-col justify-between space-y-3 rounded-lg border p-4 shadow-xs">
                <div class="flex items-start justify-between gap-3">
                  <div class="space-y-1">
                    <div class="flex min-w-0 items-center gap-2">
                      <span class="text-foreground text-sm font-semibold">Automatic Small-Model Routing</span>
                      <span ui-badge wrap variant="success" class="text-xs">Active</span>
                    </div>
                    <p class="text-muted-foreground text-xs leading-relaxed">
                      Directs intent classification, sentiment analysis, entity extraction, and structured JSON parsing to
                      GPT-4o-mini and Llama 3.3 70B instead of frontier models.
                    </p>
                  </div>
                  <button
                    ui-switch
                    [checked]="smallModelRoutingEnabled()"
                    (checkedChange)="smallModelRoutingEnabled.set($event)"
                    aria-label="Toggle small-model routing"
                  ></button>
                </div>
                <div
                  class="border-border/60 bg-muted/40 flex items-center justify-between rounded-md border px-3 py-1.5 text-xs"
                >
                  <span class="text-muted-foreground">Estimated Rule Savings</span>
                  <span class="text-success text-success font-semibold tabular-nums">
                    ~$1,240.00 / mo (68K queries rerouted)
                  </span>
                </div>
              </div>
    
              <!-- Rule 3: Semantic Vector Cache -->
              <div class="border-border bg-card flex flex-col justify-between space-y-3 rounded-lg border p-4 shadow-xs">
                <div class="flex items-start justify-between gap-3">
                  <div class="space-y-1">
                    <div class="flex min-w-0 items-center gap-2">
                      <span class="text-foreground text-sm font-semibold">Semantic Vector Caching</span>
                      <span ui-badge wrap variant="success" class="text-xs">Active</span>
                    </div>
                    <p class="text-muted-foreground text-xs leading-relaxed">
                      Evaluates embedding cosine distance on incoming queries; returns sub-30ms cached responses when
                      similarity score exceeds 0.88 threshold.
                    </p>
                  </div>
                  <button
                    ui-switch
                    [checked]="semanticCachingEnabled()"
                    (checkedChange)="semanticCachingEnabled.set($event)"
                    aria-label="Toggle semantic caching"
                  ></button>
                </div>
                <div
                  class="border-border/60 bg-muted/40 flex items-center justify-between rounded-md border px-3 py-1.5 text-xs"
                >
                  <span class="text-muted-foreground">Estimated Rule Savings</span>
                  <span class="text-success text-success font-semibold tabular-nums">
                    ~$1,676.00 / mo (144.3K queries cached)
                  </span>
                </div>
              </div>
    
              <!-- Rule 4: Fallback & Circuit Breaker Engine -->
              <div class="border-border bg-card flex flex-col justify-between space-y-3 rounded-lg border p-4 shadow-xs">
                <div class="flex items-start justify-between gap-3">
                  <div class="space-y-1">
                    <div class="flex min-w-0 items-center gap-2">
                      <span class="text-foreground text-sm font-semibold">Fallback &amp; Circuit Breaker Engine</span>
                      <span ui-badge wrap variant="success" class="text-xs">Active</span>
                    </div>
                    <p class="text-muted-foreground text-xs leading-relaxed">
                      Monitors upstream provider health; automatically reroutes degraded queries to secondary standby models
                      on HTTP 429 or &gt;1,500ms TTFT latency spikes.
                    </p>
                  </div>
                  <button
                    ui-switch
                    [checked]="circuitBreakerEnabled()"
                    (checkedChange)="circuitBreakerEnabled.set($event)"
                    aria-label="Toggle circuit breaker engine"
                  ></button>
                </div>
                <div
                  class="border-border/60 bg-muted/40 flex items-center justify-between rounded-md border px-3 py-1.5 text-xs"
                >
                  <span class="text-muted-foreground">SLA Protection</span>
                  <span class="text-foreground font-semibold tabular-nums">
                    99.99% effective uptime (226 incidents saved)
                  </span>
                </div>
              </div>
            </div>
          </ui-card-content>
        </ui-card>
      `,
    })
    export class UiModelTokenCostOptimizerComponent {
      @Input('class') className?: string
    
      protected readonly cn = cn
      protected readonly cacheSegments = cacheSegments
    
      protected readonly ArrowRightLeft = ArrowRightLeft
      protected readonly BrainCircuit = BrainCircuit
      protected readonly CheckCircle2 = CheckCircle2
      protected readonly Coins = Coins
      protected readonly Download = Download
      protected readonly Gauge = Gauge
      protected readonly Layers = Layers
      protected readonly Route = Route
      protected readonly SlidersHorizontal = SlidersHorizontal
      protected readonly Sparkles = Sparkles
      protected readonly TrendingDown = TrendingDown
      protected readonly Zap = Zap
    
      readonly timeframe = signal('aug-2026')
      readonly providerFilter = signal('all')
    
      // Active optimization rule toggles
      readonly promptCompressionEnabled = signal(true)
      readonly smallModelRoutingEnabled = signal(true)
      readonly semanticCachingEnabled = signal(true)
      readonly circuitBreakerEnabled = signal(true)
    
      readonly filteredModels = computed(() => {
        const filter = this.providerFilter()
        if (filter === 'all') return models
        return models.filter((m) => m.provider.toLowerCase().includes(filter.toLowerCase()))
      })
    
      get hostClass(): string {
        return cn('mx-auto block w-full max-w-6xl space-y-6', this.className)
      }
    
      latencyLabel(type: CacheSegmentType): string {
        return segmentLatency[type]
      }
    
      providerLabel(provider: string): string {
        return providerLabels[provider] || provider
      }
    
      /** Per-1k-request average, computed exactly like the React block. */
      avgPerThousand(model: ModelAllocation): string {
        return (parseFloat(model.totalCost.replace('$', '').replace(',', '')) / (model.sharePercent * 76.75)).toFixed(3)
      }
    }
    
  • src/app/components/blocks/model-token-cost-optimizer/index.ts0.2 kB

Raw manifest:https://uipkge.dev/r/angular/model-token-cost-optimizer.json