Envoy Gateway หน้า OpenAI พร้อมงบประมาณโทเค็นรายเดือน
ทีมgpt-4o-mini ของ OpenAI ไว้core/period-budget มี Redis เก็บ
หน้า
พื้นที่
สิ่งที่ตัวอย่างนี้บิลด์
| อินสแตนซ์ | Component ของ | สิ่ง |
|---|---|---|
edge | core/gateway/envoy-gateway | เกตเวย์ ให้assistant จากchat |
ai-controller | core/gateway/envoy-ai-gateway-operator | คอนโทรลเลอร์ |
chat | core/ai-model/openai | gpt-4o-mini ของ OpenAI เข้า |
counters | core/datastore/redis | ที่ |
assistant | core/ai-agent/crewai | เอเจนต์ |
certs | core/security/cert-manager | cert-manager ซึ่ง |
เส้นuses ของextension ไปmodels ไปcredential เป็นcache ไปmodel ของbudget โดย Aggregate เขียนtotal_tokens และgateway ซึ่ง
Manifest ผูกk8s-existing-secret ซึ่งpassword ของcache ของ
Release ประกาศcert-manager คอนโทรลเลอร์envoy-ai-gateway-system เอเจนต์assistant และenvoy-gateway-system (คอนโทรลเลอร์
cert-manager ถูกประกาศ ไม่ได้สันนิษฐาน
webhook ของCertificate ของ cert-manager และ Issuer แบบ self-signed ที่certs อยู่
คลัสเตอร์certs ออก
ร่างตัวอย่าง
คุณ
เขียน
aggrega draft --example envoy-ai-gateway-token-budgetจาก
cd envoy-ai-gateway-token-budgetบิลด์
aggrega build --release release.yaml --auto-pullRelease ต้อง
aggrega pull --release release.yaml --pin-sources # resolve once, then build with no flagaggrega build --release release.yaml --auto-pull # or resolve during the buildการ.aggrega-config.yaml ระบุ
การ--auto-pull จะ<workspace> แทน
Pull complete: release.lock (entities=26, source_artifacts=3, external_inputs=0)[… omitted: a notice that core/referenced-secret declares origin in both of its pillars, and the total build time, which differs per run]Build completed: 113 files written to <workspace>/distกับผู้ช่วย AI
ผู้
ใช้เฉพาะเครื่องมือของ Aggrega และอย่าสร้างโฟลเดอร์เอง หากโฟลเดอร์พื้นที่ทำงานยังไม่มีโฟลเดอร์ `envoy-ai-gateway-token-budget` เรียกเครื่องมือ `draft` หนึ่งครั้งโดยตั้ง `example` เป็น `envoy-ai-gateway-token-budget` และไม่ระบุ `dir` เครื่องมือจะเขียนตัวอย่างลงในโฟลเดอร์ `envoy-ai-gateway-token-budget` ใหม่ จากนั้นเรียกเครื่องมือ `build` โดยตั้ง `workspace_root` เป็น `envoy-ai-gateway-token-budget` ตั้ง `release` เป็น `release.yaml` ตั้ง `auto_pull` เป็น true และตั้ง `write` เป็น true บอกฉันว่าการบิลด์เขียนไฟล์ใดบ้าง ไฟล์เหล่านั้นอยู่ใต้ `envoy-ai-gateway-token-budget/dist/`ทรีที่เรนเดอร์
dist/helm/├── ai-controller-certificate-envoy-ai-gateway-system-self-signed-cert-for-mutating-webhook.yaml[… omitted: the controller's two cluster roles and their bindings]├── ai-controller-deployment-envoy-ai-gateway-system-ai-controller.yaml├── ai-controller-issuer-envoy-ai-gateway-system-self-signed-issuer-for-mutating-webhook.yaml├── ai-controller-mutatingwebhookconfiguration-envoy-ai-gateway-gateway-pod-mutator-envoy-ai-gateway-system.yaml├── ai-controller-service-envoy-ai-gateway-system-ai-controller.yaml[… omitted: the controller's service account]├── ai-controller/│ ├── aigateway.envoyproxy.io_aigatewayroutes.yaml[… omitted: the other five AI Gateway resource definitions]├── assistant-deployment.yaml[… omitted: the files cert-manager's chart renders, each named certs-…]├── chat-service.yaml├── counters-default-service.yaml├── counters-deployment.yaml[… omitted: Envoy Gateway's cluster roles and their bindings]├── edge-configmap-envoy-gateway-system-envoy-gateway-config.yaml[… omitted: the resource definitions Envoy Gateway's chart installs]├── edge-deployment-envoy-gateway-system-envoy-gateway.yaml[… omitted: the rest of Envoy Gateway's chart — its certificate job, admission configurations, roles, bindings, Service and service accounts]└── edge/ ├── gateway.yaml ├── gatewayclass.yaml └── models.yamlงบdist/helm/edge/models.yaml ซึ่งdist/helm/edge-configmap-envoy-gateway-system-envoy-gateway-config.yaml
งบประมาณตามที่เกตเวย์อ่าน
จาก models.yaml ซึ่ง
apiVersion: aigateway.envoyproxy.io/v1beta1kind: AIGatewayRoutemetadata: [… omitted: the labels and annotations every rendered resource carries] name: edge-models namespace: "envoy-gateway-system"spec: parentRefs: - name: edge kind: Gateway group: gateway.networking.k8s.io llmRequestCosts: - metadataKey: llm_total_token type: TotalToken rules: - matches: - headers: - type: Exact name: x-ai-eg-model value: "assistant" backendRefs: - name: edge-assistant modelNameOverride: "gpt-4o-mini"และ
apiVersion: gateway.envoyproxy.io/v1alpha1kind: BackendTrafficPolicymetadata: [… omitted: the labels and annotations every rendered resource carries] name: edge-models namespace: "envoy-gateway-system"spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: edge-models rateLimit: global: rules: - limit: requests: 20000000 unit: Month shared: true cost: request: from: Number number: 0 response: from: Metadata metadata: namespace: io.envoy.ai_gateway key: llm_total_tokenนี่envoy-gateway.yaml ของ
apiVersion: gateway.envoyproxy.io/v1alpha1kind: EnvoyGatewayextensionApis: enableBackend: true enableEnvoyPatchPolicy: trueextensionManager: hooks: xdsTranslator: post: - Translation - Cluster - Route translation: cluster: includeAll: true listener: includeAll: true route: includeAll: true secret: includeAll: true service: fqdn: hostname: ai-controller.envoy-ai-gateway-system.svc.cluster.local port: 1063gateway: controllerName: gateway.envoyproxy.io/gatewayclass-controllerlogging: level: default: infoprovider: kubernetes: rateLimitDeployment: container: env: - name: REDIS_AUTH valueFrom: secretKeyRef: key: password name: counter-store-password - name: USE_CALENDAR_MONTH_RATE_LIMIT value: "true" image: docker.io/envoyproxy/ratelimit:8fe6ea42 patch: type: StrategicMerge value: spec: template: spec: containers: - imagePullPolicy: IfNotPresent name: envoy-ratelimit shutdownManager: image: docker.io/envoyproxy/gateway:v1.9.1 type: KubernetesrateLimit: backend: redis: url: counters-default.envoy-gateway-system.svc.cluster.local:6379 type: Redis failClosed: trueสิ่ง
ต้นทุนtotal_tokens ที่llm_total_token ซึ่งllmRequestCosts ประกาศ คอนโทรลเลอร์edge-models และ
การcost.request ไม่cost.response คิดlimit
การshared: true เก็บgateway ที่scope: model Recipe จะmodels โดยx-ai-eg-model จึง
ช่วง1mo ถูกunit: Month และUSE_CALENDAR_MONTH_RATE_LIMIT ให้
การcache บล็อก rateLimit เปิดcache ของ
การfailClosed: true ทำให้ Envoy ปฏิเสธ
ไม่มีREDIS_AUTH จาก Secret ที่cache ระบุ คือ counter-store-password ซึ่งcounters-deployment.yaml เริ่ม--requirepass ที่
เส้นทางจากเอเจนต์ไปยังโมเดล
จาก assistant-deployment.yaml ซึ่งenv ของ
env: - name: OPENAI_BASE_URL value: http://envoy-envoy-gateway-system-edge-06431a46.envoy-gateway-system.svc.cluster.local:80/v1 - name: MODEL value: assistant - name: CREWAI_ALLOW_DELEGATION value: "false" - name: CREWAI_BACKSTORY value: a help-desk agent answering customers - name: CREWAI_GOAL value: answer questions about the product - name: CREWAI_LOG_LEVEL value: DEBUG - name: CREWAI_ROLE value: support assistantเอเจนต์CREWAI_ มีenvoy-gateway-system กำกับ จึงassistant โดย route ส่งgpt-4o-mini แทน
backend เข้าmodels.yaml มี Backend ของ Envoy Gateway สำหรับ api.openai.com ที่AIServiceBackend สำหรับ OpenAI API ภาย/v1 และedge-assistant ซึ่ง spec ของ
spec: targetRefs: - group: aigateway.envoyproxy.io kind: AIServiceBackend name: edge-assistant type: APIKey apiKey: secretRef: name: "openai-api-key"คอนโทรลเลอร์apiKey และextensionManager ข้างedge-client-buffer-limit ใน models.yaml ซึ่ง
สิ่งที่เกตเวย์ทำขณะรัน
ในlimit หรือfailClosed ข้าง
สิ่ง
- คำขอ
ที่ โทเค็นได้ รับ อนุญาต ก่อน เกิด การ ล่ม ไม่ นาน ยัง ได้ รับ บริการ และ ไม่ ถูก นับ ของ คำขอ ถูก คิด ค่า หลัง การ ตอบ กลับ การ ตอบ กลับ ที่ เสร็จ สมบูรณ์ ขณะ ที่ เข้า ถึงที่ จัด เก็บ หรือ บริการ ไม่ ได้ จึง ไม่ เพิ่ม สิ่ง ใด ให้การ นับ - ที่
จัด เก็บ ที่ เก็บ การ นับ ไว้ ใน หน่วย ความ จำ จะ เริ่ม ทุก ช่วง ใหม่ เมื่อ รีสตาร์ต core/datastore/redisเก็บรายการ ไว้ ใน หน่วย ความ จำ โดย ค่า เริ่ม ต้น ที่ จัด เก็บ ที่ รีสตาร์ต จึง สูญ เสีย การ นับ ที่ เก็บ ไว้ จนถึง ตอน นั้น และ ส่วน ที่ เหลือ ของ เดือน ถูก นับ จาก ศูนย์ - การ
ตอบ การกลับ ที่ ไม่ รายงาน การ ใช้ งาน ไม่ เพิ่ม สิ่ง ใด นับ คือ สิ่ง ที่ การ ตอบ กลับ แต่ละ ครั้ง รายงาน Envoy AI Gateway ขอ ให้ โมเดล รายงาน การ ใช้ งาน ท้าย การ ตอบ กลับ แบบ สตรีม ด้วย
อะไรที่ทำให้บิลด์หยุด
แต่ละ
-
ต้นทุน
เป็น ตั้งดอลลาร์ สหรัฐ unit: usdในการ แนบ การ บิลด์ จะ หยุด ก่อน ที่ สิ่ง ใด จะ เรนเดอร์: [… omitted: what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0033]Recipe/FeatureRecipe assertionscaused by: FeatureRecipe "period-budget-envoy-gateway" instance "edge" assertion "unit_is_a_token_count": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: core/period-budget attachment "budget" on "edge" asks for unit usd, but core/gateway/envoy-gateway, which declares dataplane.aggrega.ai/period-budget: tokens, counts the tokens each model's reply reports and prices nothing, so it keeps no budget in US dollars; set unit to total_tokens, prompt_tokens or completion_tokens, or attach the Feature to a gateway that counts cost -
งบ
ประมาณ คือต่อ ผู้ เรียก scope: callerเกตเวย์แยก ผู้ เรียก ออก จาก กัน ได้ ด้วย เฮดเดอร์ ที่ ผู้ เรียก เขียน เอง เท่านั้น และ ไม่มี สิ่ง ใด บน เส้น ทาง นี้ ยืนยัน ว่า ผู้ เรียก คือ ใคร ผู้ เรียก ที่ ส่ง ค่า ใหม่ จึง เริ่ม งบ ประมาณ ใหม่ และ ผู้ เรียก ที่ ไม่ ส่ง ค่า ใด จะ ไม่ ถูก นับ -
ช่วง
เวลา เกตเวย์ใด ก็ตาม ที่ เกตเวย์ ไม่มี ช่วง รองรับ รับ หนึ่ง หน่วย เต็ม ของ ขีด จำกัด อัตรา ได้แก่ 1s,1m,1h,1d,7d,30d,365dหรือ1moหรือความ ยาว เดียวกัน ที่ สะกด แบบ อื่น เช่น 24hส่วน2h,14dหรือ2moจะหยุด -
ขีด
จำกัด หรือที่ ไม่ใช่ จำนวนเต็ม ของ โทเค็น ขีด จำกัด ที่ สูง กว่า 4294967295 -
การ
แนบ core/period-budgetรายการที่ ซึ่งสอง บน เกตเวย์ เขียน งบ ประมาณ หนึ่ง รายการ ลง บน route ที่ ให้ บริการ โมเดล ของ เกตเวย์ -
ไม่มี
สิ่ง ใด เชื่อม โยง กับ ซ็อกเก็ต modelsซึ่งทำให้ หรือไม่มี route ของ โมเดล ให้ นับ กับ ซ็อกเก็ต cacheซึ่งทำให้ การ นับ ไม่มี ที่ อยู่ ลบ เฉพาะ การ เชื่อม ต่อ ของ โมเดล ออก แล้ว Component ของ เกตเวย์ เอง จะ หยุด ก่อน เพราะ values.modelระบุนามแฝง ที่ ไม่มี สิ่ง ใด ให้ บริการ -
ที่
จัด เก็บ บน ซ็อกเก็ต cacheที่ไม่ คือใช้ โปรโตคอล Redis ที่ จัด เก็บ ที่ เผย แพร่ โปรโตคอล อื่น ที่ ไม่ใช่ redisหรือvalkey -
เกตเวย์
ที่ วาง นอก envoy-gateway-systemขณะที่ คอนโทรลเลอร์ เชื่อม โยง กับ ซ็อกเก็ต extensionของเกตเวย์
ก่อนนำไปใช้
ทรีdist/helm/ จึง
- เนมสเปซ
cert-manager,envoy-ai-gateway-system,envoy-gateway-systemและassistant - Secrets ใน
envoy-gateway-systemได้แก่openai-api-keyซึ่งเก็บ คีย์ ของ OpenAI ภาย ใต้ คีย์ ข้อมูล apiKeyและcounter-store-passwordซึ่งเก็บ รหัส ผ่าน ของ ที่ จัด เก็บ ภาย ใต้ passwordเวิร์กโหลดที่ อ้างอิง Secret ที่ ไม่มี อยู่ จะ ไม่ เริ่ม ทำงาน - server-side apply สำหรับ
นิยาม คือของ AI Gateway สอง รายการ aigateway.envoyproxy.io_backendsecuritypolicies.yamlและaigateway.envoyproxy.io_mcproutes.yamlซึ่งมี ขนาด ใหญ่ กว่า สำเนา ของ ออบเจ็กต์ ที่ นำ ไป ใช้ ซึ่ง client-side apply บันทึก ไว้ client-side apply จึง ปฏิเสธ นิยาม เหล่า นั้น - อิมเมจ
สำหรับ Release ระบุเอเจนต์ registry.example/crew-assistant:1.0.0แค็ตตาล็อกจัด แพ็กเกจ รันไทม์ ของ เอเจนต์ และ crew ที่ เอเจนต์ รัน คือ โค้ด ของ คุณ เอง
ตำแหน่งที่คอมไพเลอร์ไม่ตีความ
คอมไพเลอร์
การปรับแต่งตัวอย่างนี้
- ขีด
จำกัด เชื่อมต่อ โมเดล โยง โมเดล เพิ่ม เข้า กับ ซ็อกเก็ต modelsและตั้ง scope: modelแต่ละโมเดล จะ ได้ กฎ และ การ นับ ของ ตัว เอง - นับ
เพียง ตั้งฝั่ง เดียว unit: prompt_tokensเพื่อนับ สิ่ง ที่ โมเดล อ่าน หรือ completion_tokensสำหรับสิ่ง ที่ โมเดล เขียน - ส่ง
คำขอ แนบที่ ล้ม เหลว อีก ครั้ง core/retry-policyกับเกตเวย์ พร้อม max_attemptsและแมป ใน Manifest ไป ยัง helm/retry-policy-envoy-gatewayนโยบายทราฟฟิก เดียวกัน คือ edge-modelsจะได้ retryเพิ่มข้าง ขีด จำกัด อัตรา เพราะ Envoy Gateway แนบ นโยบาย ทราฟฟิก หนึ่ง รายการ กับ route หนึ่ง - ช่วง
ที่ ตั้งสั้น กว่า หรือ มี ความ ยาว คงที่ periodใน Release เป็นหนึ่ง ใน ความ ยาว ข้าง ต้น 1dคือหนึ่ง วัน เริ่ม จาก 00:00 UTC และ 30dคือ 30 วันที่ นับ จาก 1 มกราคม 1970 ไม่ใช่ เดือน ตาม ปฏิทิน เมื่อ ใช้ ช่วง เวลา ใด ก็ตาม ที่ ไม่ใช่ 1moการกำหนด ค่า จะ ไม่ ตั้ง ค่า เดือน ตาม ปฏิทิน - MCP route คอนโทรลเลอร์
เข้า รหัส ตัว ระบุ เซสชัน MCP ภาย ใต้ คีย์ ที่ ได้ มา จาก seed ใน Secret ที่ infra.mcp_session_seed_secretระบุบน ai-controllerเมื่อไม่ ได้ ระบุ เช่น ใน ที่ นี้ seed คือ ค่า เริ่ม ต้น ที่ เผย แพร่ ของ chart ของ คอนโทรลเลอร์ จึง ควร ระบุ หนึ่ง รายการ ก่อน ให้ บริการ MCP route ผ่าน คอนโทรลเลอร์