شعار Ai Model Gateway

Ai Model Gateway

المجتمع
peterbamuhigire
ai-model-gateway

استخدم هذه المهارة عند تصميم بوابة LLM أو بنائها لتجريد مزودي الخدمة، والتوجيه المراعي للمستأجرين، والبدائل الاحتياطية، والحصص، وتوطين البيانات، وسجلات التدقيق، وتسجيل التكاليف، وتطبيق مفاتيح الإيقاف.

Add to TypingMind

نظرة عامة

الناشرpeterbamuhigire
المستودعchwezi-dev-engine
اسم المهارةai-model-gateway
النجوم
29
التفريعات
6
الملفات المرفقة
3
الترخيصMIT
الروابط
  • تعليمات بصيغة Markdown

    ملف SKILL.md يُحمّله النموذج عند الحاجة، فلا يستهلك توكنات إلا عندما يَرِد طلب ذو صلة فعلًا.

  • تعمل مع أي نموذج لغوي كبير

    مهارات الذكاء الاصطناعي مكتوبة بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا تعمل مع GPT أو Claude أو Gemini أو Grok أو نموذج محلي.

  • الملفات المرفقة: 3

    نصوص برمجية وقوالب ومراجع يمكن للنموذج قراءتها أثناء عمله. الملفات للقراءة فقط ولا تُنفّذ أبدًا.

  • مفتوحة المصدر

    نشرها peterbamuhigire على GitHub. اطّلع على المصدر قبل تثبيتها.

التثبيت

ثبّت مهارة الذكاء الاصطناعي Ai Model Gateway في TypingMind لاستخدامها مع أي نموذج لغوي كبير، أو أضفها إلى وكيل ذكاء اصطناعي آخر يقرأ SKILL.md.

1

التثبيت في TypingMind

يُثبّت TypingMind المهارة مباشرةً من مجلدها على GitHub — فيقرأ SKILL.md، ويجمع ملفات الموارد في حزمة، ويحفظ النتيجة محليًا.

  1. افتح التطبيق وانتقل إلى «الإضافات» ثم «المهارات».
  2. اختر «تثبيت من GitHub».
  3. الصق رابط مجلد المهارة الموضّح أدناه وأكّد التثبيت.
    https://github.com/peterbamuhigire/chwezi-dev-engine/tree/main/skills/ai/ai-model-gateway
  4. فعّل المهارة في كل محادثة تريد إتاحتها فيها.
الإضافات ← المهارات ← إضافة مهارة ← من رابط GitHub، ثم الصق رابط المجلد واضغط على «متابعة».
2

التثبيت في وكيل ذكاء اصطناعي آخر

يمكن لأي وكيل ذكاء اصطناعي يقرأ تنسيق Agent Skills استخدام هذه المهارة — انسخ المجلد إلى مجلد المهارات الخاص بذلك الوكيل.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/peterbamuhigire/chwezi-dev-engine.git /tmp/chwezi-dev-engine
mkdir -p .claude/skills
cp -r /tmp/chwezi-dev-engine/skills/ai/ai-model-gateway .claude/skills/ai-model-gateway
أعد تشغيل Claude Code بعد النسخ ليتعرّف على المهارة الجديدة.

استخدمها في TypingMind

فعّل Ai Model Gateway في أي محادثة على TypingMind وسيتولى النموذج الباقي. تتضمن مطالبة النظام اسمها ووصفها، وبمجرد ورود طلب ذي صلة، يُحمّل النموذج التعليمات الكاملة تلقائيًا — فلا تحتاج إلى استدعائها يدويًا، ولا تستهلك أي توكنات حتى تُستخدم فعليًا.

يُحمّل النموذج مهارة Ai Model Gateway تلقائيًا بمجرد ورود طلب ذي صلة بها.

تعمل مع أي نموذج ذكاء اصطناعي

مهارات الذكاء الاصطناعي هي تعليمات بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا لا ترتبط Ai Model Gateway بالنموذج الذي كُتبت له. ثبّتها مرة واحدة في TypingMind واستخدمها مع GPT-5 أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو نموذج محلي تشغّله بنفسك — كل ذلك باستخدام مفاتيح API الخاصة بك.

  • تُحمَّل عند الحاجة فقط

    تتضمن مطالبة النظام الاسم والوصف فقط. وتُجلب التعليمات عند أول طلب ذي صلة، لذا لا تستهلك تعليمات المهارة أي توكنات ما دامت المهارة غير مستخدمة.

  • بدّل النماذج أثناء المحادثة

    لأن المهارة تعليمات وليست شيفرة، فإن تغيير النموذج لا يعطّلها — فالنموذج التالي يقرأ ملف SKILL.md نفسه.

تعليمات المهارة

هذا هو محتوى SKILL.md الذي يُحمّله النموذج. اقرأه قبل التثبيت — فالمهارة عبارة عن تعليمات سيتبعها نموذجك.

AI Model Gateway

Operating contract

Inputs

InputRequiredPurpose
Domain evidenceyesprovider inventory, tenant plans, residency constraints, request schema, budgets, fallback policy, and kill-switch owner

Outputs

  • Produce: gateway API contract, routing table, quota enforcement, audit/cost events, fallback chain, and operational runbook.

Capability and permission boundaries

Default to read-only analysis. Read only scoped records; redact secrets and regulated data. Writes, execution, network calls, production configuration, customer communication, billing changes, and delegation require explicit authority and an identified owner. Never widen tenant, time-window, or system scope implicitly.

Degraded mode

When required telemetry, evidence, execution, network access, or write authority is unavailable, return a partial result with each unassessed item labelled, preserve the safest existing state, and state the evidence or approval needed to continue. Never convert missing evidence into a pass.

Decision rules

ConditionAction
Scope, owner, or threshold is missingStop the affected decision and request it
Evidence is incomplete but read-only analysis is safeProduce a qualified partial result and gap list
A mutation exceeds authority or tenant boundaryBlock it and route for approval
Evidence meets the stated thresholdIssue the output with provenance and owner

Anti-Patterns

  • Treating absent evidence as success. Fix: mark the check unassessed and name the missing source.
  • Expanding one tenant or workflow to all tenants. Fix: enforce supplied scope at every query and action.
  • Performing a production write during analysis. Fix: emit a reviewed change plan until authority is explicit.
  • Reporting a metric without population, window, or source. Fix: attach all three.
  • Hiding a failed threshold inside an average. Fix: report failure slices and the remediation owner.

Acknowledgement: Shared by Peter Bamuhigire, techguypeter.com, +256 784 464178.

Use When

  • Designing or implementing the LLM gateway as a control-plane service for a multi-tenant SaaS.
  • Replacing direct OpenAI / Anthropic / Bedrock SDK calls in feature code with a routed, audited, cost-attributed path.
  • Adding multi-provider fallback to an existing AI feature (Anthropic → Bedrock → OpenAI).
  • Enforcing per-tenant token caps, regional routing, and audit at one chokepoint instead of scattered library calls.

Do Not Use When

  • The task is the wider AI architecture — start with ai-on-saas-architecture.
  • The task is direct provider API exploration / spike — ai-llm-integration is the bare-metal SDK skill.
  • The task is the prompt design — use ai-prompt-engineering.

Required Inputs

  • The model tier catalogue (Free → distilled; Pro → mid-tier; Enterprise → flagship).
  • The provider list, their SLAs, their regions, and contract limits.
  • The tenant_ai_binding schema from ai-on-saas-architecture.
  • Cost ceiling policy per plan (hard caps vs soft caps).
  • Residency commitments per tenant region.

Book-derived additions

When the gateway is part of a wider AI data foundation, load AI data foundation selection and operations for data-product contracts, portability, trust, and session-level operations.

Workflow

  1. Read this SKILL.md.
  2. Define the internal gateway contract (§1) — HTTP + SDK shape, request envelope, response envelope.
  3. Implement the request pipeline (§2): auth → model resolve → rate limit → safety in → provider call → safety out → cost capture → audit → respond.
  4. Build the provider adapter layer (§3) so adding a provider is a < 200-LOC change.
  5. Add fallback chains (§4) per tier.
  6. Add per-tenant token/USD ceilings (§5).
  7. Add regional routing (§6) for residency.
  8. Wire audit + cost capture (§7) at write time.
  9. Implement the kill-switch path (§8).
  10. Document SLA and ops (§9).
  11. Apply anti-patterns (§10).

Quality Standards

  • Adding a new feature requires zero code in the gateway beyond a prompt-id registration.
  • Adding a new provider requires < 200 LOC and zero feature-code changes.
  • Hot-path overhead added by the gateway < 50ms p95 over raw provider call.
  • 100% of AI requests are audited and cost-attributed at request close — never reconciled from invoices.
  • Kill-switch flip propagates in < 60 seconds.
  • Gateway is the only outbound path; a lint rule rejects PRs that import provider SDKs outside the gateway repo.

Anti-Patterns

  • Gateway with feature-specific code paths inside it (gateway becomes a god service).
  • No fallback chain — primary provider 429 brings the whole product down.
  • Cost computed nightly from logs instead of at request close — tenants get billed days late.
  • Rate limit only at the provider layer — one tenant exhausts the global key and noises everyone.
  • Audit log writes are best-effort (lost on crash) — compliance and billing diverge.
  • Hard caps without a degraded-mode option — tenants hit the cap and the whole product breaks.
  • Gateway leaks provider-specific error shapes to feature code — re-coupling.

Outputs

  • Gateway HTTP contract + SDK in N languages used by feature teams.
  • Provider adapter set with capability matrix.
  • Fallback policy per tier.
  • Token / USD ceiling policy per plan.
  • Region routing table.
  • Audit log + cost ledger schema (lives in audit skill).
  • Kill-switch UX + back-office wiring (saas-admin-backoffice-tooling).

Evidence Produced

CategoryArtifactFormatExample
CorrectnessGateway contractOpenAPI docdocs/ai/gateway-api.yaml
CorrectnessProvider capability matrixMarkdown tabledocs/ai/provider-matrix.md
Release evidenceFallback policy per tierMarkdown docdocs/ai/fallback-policy.md
OperabilityGateway SLO + on-call runbookMarkdown runbookdocs/runbooks/llm-gateway.md

References

  • references/llm-gateway-design.md — full design (canonical copy lives in ai-on-saas-architecture/references/).
  • references/token-accounting-pipeline.md — how token-in / token-out / cost rolls up.
  • Companion: ai-on-saas-architecture, ai-cost-per-tenant-attribution, ai-usage-metering-and-billing, saas-entitlements-and-plan-gating, ai-prompt-injection-and-tenant-safety, ai-observability-and-debugging, saas-rate-limiting-and-quotas.
  • Incident primitives: the gateway is the surface that exposes the operator primitives an on-call uses during an incident — kill-switch (feature/agent task), model-pin, prompt-pin, gateway routing pin, per-tenant feature pause, quota cap. Each primitive must propagate in < 60s, log to ai_incident_mitigation_log with (actor, ts, primitive, scope, reason, ticket_id), and be invocable from a back-office UI without writing code or SQL. See ai-incident-response-runbook §3 for the full primitive contract and ai-incident-recovery-and-rollback/references/rollback-patterns.md for the un-pin contract.

§1 Internal Gateway Contract

A single endpoint that feature teams call:

POST /v1/generate
Authorization: Bearer <service-jwt>     # signed by internal auth
X-Tenant-Id: 8421                       # required
X-Feature-Id: support-copilot.answer    # namespaced
X-Trace-Id: trc_01HXY...                # propagate

{
  "prompt_id": "support.answer",
  "prompt_version": "latest",          // or pinned e.g. "v17"
  "variables": { "user_question": "...", "kb_partition_id": "kb_t8421" },
  "retrieval": {                       // optional; gateway can call KB service
    "do_retrieve": true,
    "top_k": 6
  },
  "intent": "answer_question",
  "user_id": 990012,
  "max_tokens_out": 800,
  "temperature": 0.2,
  "stream": false
}

Response:

json
{
  "request_id": "ai_req_01HXY...",
  "model_used": "claude-3.7-sonnet",
  "region": "eu-west-1",
  "text": "...",
  "tokens_in": 1840,
  "tokens_out": 412,
  "usd_cost": 0.013824,
  "latency_ms": 1923,
  "fallback_used": false,
  "safety_findings": [],
  "grounding_score": 0.91,
  "citations": [{"chunk_id": "...", "source": "...", "score": 0.83}],
  "eval_sampled": false
}

A streaming variant uses Server-Sent Events. The final SSE event carries the full envelope (cost, latency, model, audit id).

§2 Request Pipeline

1. Authn (service JWT)        — verify signature; resolve calling service
2. Authz (tenant + feature)   — service is allowed to act for this tenant on this feature
3. Resolve binding            — read tenant_ai_binding for tenant
4. Entitlement check          — tenant's plan permits this feature/model
5. Kill-switch check          — ai_enabled = false → 403 fast
6. Rate limit                 — Redis token bucket per tenant per feature
7. Cap check                  — monthly USD/token cap not exceeded
8. Resolve prompt             — prompt registry returns (template, model_hint)
9. Render prompt              — template + variables (sanitised)
10. Safety in                 — prompt-injection classifier on user-supplied variables
11. (optional) Retrieval      — call KB service with tenant_id (no other path)
12. Provider call             — primary; retry once on transient
13. Safety out                — PII scrub, jailbreak detect, grounding check
14. Cost compute              — tokens × price table → usd_cost
15. Audit write               — synchronous; row in ai_requests
16. Cost event                — ai.cost.recorded onto event bus
17. Eval sample               — N% of requests written to eval queue
18. Respond                   — envelope to caller

The pipeline is the gateway. Each stage has a hard timeout; stage failures emit gateway.stage.failed traces.

§3 Provider Adapter Layer

python
class Provider(Protocol):
    name: str
    models: list[ModelDescriptor]
    regions: list[str]

    async def generate(self, req: NormalizedRequest) -> NormalizedResponse: ...

class AnthropicProvider:
    name = "anthropic"
    models = [
        ModelDescriptor("claude-3.7-sonnet", ctx=200_000,
                        in_price=3e-6, out_price=15e-6),
        ModelDescriptor("claude-3-haiku", ctx=200_000,
                        in_price=0.25e-6, out_price=1.25e-6),
    ]
    regions = ["us-east-1", "eu-west-1"]

    async def generate(self, req): ...

Adapter responsibilities:

  • Translate normalised request → provider SDK call.
  • Translate provider response/error → normalised response/error.
  • Surface model capability flags (vision, tools, JSON-mode, streaming).
  • Report region routing options.

Anything else (rate limit, retries with backoff, cost compute, audit) lives in the pipeline, not the adapter.

§4 Fallback Chains

Per tier, an ordered list of (provider, model, region) candidates.

yaml
tiers:
  enterprise:
    primary:     [anthropic, claude-3.7-sonnet, region:tenant]
    fallback_1:  [bedrock,   anthropic.claude-3-5-sonnet, region:tenant]
    fallback_2:  [openai,    gpt-4o, region:tenant_or_us]
  pro:
    primary:     [anthropic, claude-3.7-sonnet, region:tenant]
    fallback_1:  [anthropic, claude-3-5-haiku, region:tenant]
  free:
    primary:     [anthropic, claude-3-haiku, region:any]
    fallback_1:  [openai,    gpt-4o-mini, region:any]

Triggering fallback:

  • 5xx, timeout, or RateLimitError after one retry on primary.
  • 429 with Retry-After > slo_budget.
  • Model deprecation event.
  • A safety vote from the in-line classifier (rare).

Record fallback_used=true in the audit row; alert when fallback ratio for a tier exceeds threshold.

§5 Per-Tenant Token / USD Ceilings

The gateway enforces caps at the atomic check-and-increment level using Redis (saas-rate-limiting-and-quotas algorithms). Three ceilings:

  • Hard USD cap per month (tenant_ai_binding.monthly_usd_cap). On hit: 429 + quota:ai_usd. Upgrade path link in response.
  • Hard token cap per day (rate-shaped). On hit: 429 + quota:ai_tokens_day.
  • Soft cap at 80% — fires ai.budget.threshold event for in-product banner and sales-assist email; no enforcement.

For enterprise tenants on a true-up model, replace the hard cap with a paging threshold instead.

§6 Regional Routing

tenant_ai_binding.region drives:

  • The provider region called (must match for residency).
  • The region of the KB partition called.
  • The S3 bucket of the audit payload.

When a region's preferred model is unavailable, the gateway consults a region policy:

  • strict: 503 if the region cannot serve. Used for sovereignty.
  • degraded: allow cross-region with a region_breach=true flag in audit; emit alert.
  • permissive: allow cross-region silently (default for low-sensitivity tenants).

§7 Audit + Cost Capture

Synchronous, in-pipeline, atomic with the response. The gateway returns ONLY after the ai_requests row is committed and the ai.cost.recorded event has been published (or rolled back).

Two writes:

  1. Postgres ai_requests row (the legal/compliance record).
  2. Redis tenant cost counter increment (the realtime billing view).

A reconciliation job nightly compares Postgres rollups vs Redis to detect drift.

See references/token-accounting-pipeline.md.

§8 Kill-Switch Path

The gateway reads tenant_ai_binding.ai_enabled from a Redis-cached binding (TTL 30s; invalidated by ai.kill_switched event).

On ai_enabled = false:

json
{
  "error": {
    "code": "AI_DISABLED",
    "message": "AI features are disabled for this tenant. Contact support.",
    "kill_switch_reason": "tenant requested temporary disable"
  }
}

The back-office UI (saas-admin-backoffice-tooling) exposes the toggle plus a feature-scoped variant (tenant_ai_feature_disable table).

§9 SLA & Ops

MetricTarget
Gateway availability99.95%
Hot-path overhead< 50ms p95
Audit-write success100% (any failure = reject request)
Time to roll a new provider< 1 week from contract to traffic
Time to flip kill-switch< 60 seconds
Fallback ratio (enterprise tier)< 1% sustained

Dashboards: gateway QPS, p50/p95/p99 latency by stage, error rate by stage, fallback ratio by tier, cost burn by tenant top-20.

§10 Anti-Patterns

  • Building per-feature gateway routes — turns the gateway into a feature factory.
  • Streaming responses that don't emit a final envelope event — cost + audit lost on disconnect.
  • Async audit writes via best-effort queue — failures cause compliance and billing drift.
  • Rate limiting only at the provider — noisy tenants block others.
  • Logging full prompts and responses unfiltered — PII exposure; encrypt audit payloads at rest.
  • One provider, one model — first outage = total outage.
  • Adapter logic leaking into the pipeline (provider-specific if anthropic: ... branches).

§11 Read Next

  • ai-on-saas-architecture — broader context.
  • ai-cost-per-tenant-attribution — what the gateway feeds.
  • ai-usage-metering-and-billing — how the ledger turns into invoices.
  • ai-prompt-injection-and-tenant-safety — the safety-in/safety-out logic the pipeline runs.
  • ai-observability-and-debugging — traces, replays, debugging.
  • saas-rate-limiting-and-quotas — algorithms the gateway uses.

الملفات المرفقة

يقرأ النموذج هذه الملفات عند الحاجة ما دامت المهارة محمّلة. وهي متاحة للقراءة ولا تُنفّذ أبدًا.

الأسئلة الشائعة

ماذا تفعل مهارة الذكاء الاصطناعي Ai Model Gateway؟

استخدم هذه المهارة عند تصميم بوابة LLM أو بنائها لتجريد مزودي الخدمة، والتوجيه المراعي للمستأجرين، والبدائل الاحتياطية، والحصص، وتوطين البيانات، وسجلات التدقيق، وتسجيل التكاليف، وتطبيق مفاتيح الإيقاف.

لماذا تستخدم Ai Model Gateway على TypingMind؟

لأنك تثبّتها مرة واحدة وتستخدمها مع أي نموذج. مهارة Ai Model Gateway عبارة عن نص Markdown وليست شيفرة خاصة بمزود بعينه، لذا تعمل المهارة نفسها مع GPT-5 أو Claude أو Gemini أو Grok أو نموذج محلي، ويمكنك تبديل النموذج أثناء المحادثة دون تعطّل المهارة. يعمل TypingMind بمفاتيح API الخاصة بك، فتدفع للمزودين مباشرةً بدلًا من اشتراك لكل مقعد، وتبقى مهاراتك ومحادثاتك في مساحة التخزين الخاصة بك.

كيف أثبّت Ai Model Gateway في TypingMind؟

في TypingMind، افتح «الإضافات»، ثم «المهارات»، ثم اختر «التثبيت من GitHub» والصق https://github.com/peterbamuhigire/chwezi-dev-engine/tree/main/skills/ai/ai-model-gateway. يقرأ TypingMind ملف SKILL.md الخاص بالمهارة، ويجمع ملفاتها ويثبّتها كمهارة يمكنك تفعيلها لكل محادثة على حدة.

ما نماذج الذكاء الاصطناعي التي يمكنها استخدام Ai Model Gateway؟

أي نموذج تربطه في TypingMind. مهارات الذكاء الاصطناعي عبارة عن تعليمات Markdown وليست شيفرة خاصة بمزود بعينه، لذا يمكن لكل من GPT وClaude وGemini وGrok والنماذج المحلية تحميل هذه المهارة عندما يتوافق الطلب معها.

كم نموذج ذكاء اصطناعي يمكنني استخدامه مع Ai Model Gateway؟

أي عدد تريده. ما دام النموذج يدعم المهارات، يمكنك استخدام Ai Model Gateway معه، سواء كان GPT أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو غيرها، وكل ذلك على TypingMind باستخدام مفاتيح API الخاصة بك.

هل مهارة الذكاء الاصطناعي Ai Model Gateway مجانية؟

نعم. نشرها peterbamuhigire على GitHub بموجب ترخيص MIT. لا تدفع إلا لمزود الذكاء الاصطناعي الخاص بك مقابل التوكنات التي تستخدمها.

ما هي مهارات الذكاء الاصطناعي؟

مهارة الذكاء الاصطناعي حزمة تعليمات قابلة لإعادة الاستخدام تعلّم نموذج الذكاء الاصطناعي كيفية تنفيذ مهمة محددة. وتتبع تنسيق Agent Skills المفتوح: ملف SKILL.md يتضمن اسمًا ووصفًا، إلى جانب أي نصوص برمجية أو قوالب أو ملفات مرجعية قد يحتاج إليها النموذج. لا يقرأ النموذج التعليمات إلا عندما يتوافق طلبك مع المهارة، لذا لا تترتب على المهارة المثبتة أي تكلفة حتى تُستخدم.

كيف تختلف مهارات الذكاء الاصطناعي عن الإضافات أو موصلات MCP؟

تمنح الإضافة أو موصل MCP النموذج أدوات جديدة يمكنه استدعاؤها، وهي شيفرة تُنفَّذ في مكان ما وتُرجع نتيجة. أما مهارة الذكاء الاصطناعي فتزوّد النموذج بالمعرفة ومنهجية العمل: كيفية التعامل مع مهمة، والخطوات التي ينبغي اتباعها، وما ينبغي أن تكون عليه المخرجات الجيدة. المهارات عبارة عن نصوص Markdown، لذا لا تحتاج إلى خادم أو مفتاح API أو بيئة تشغيل، وتعمل مع أي نموذج.

عرض الكل

أنشئ مساحة عملك للذكاء الاصطناعي الآن

اشترك في نشرتنا الإخبارية لتصلك أخبار الميزات الجديدة والهدايا القادمة 👇