您的 AI 网关支持不同的策略来处理对提供商的请求,这允许您有效管理 AI 交互并确保您的应用保持响应性和可靠性。
请求超时允许您在提供商响应时间过长时触发回退或重试。
这些超时有助于:
- 通过防止用户等待响应时间过长来改善用户体验
- 通过检测无响应的提供商并触发回退选项来主动处理错误
请求超时可以在通用端点上设置,也可以直接在对任何提供商的请求上设置。
超时以毫秒为单位设置。此外,超时基于响应的第一部分何时返回。只要响应的第一部分在指定的时间范围内返回 - 例如在流式传输响应时 - 您的网关将等待响应。
如果在通用端点上设置,请求超时指定请求的超时持续时间并触发回退。
对于通用端点,通过在提供商特定的 config
对象中设置 requestTimeout
属性来配置超时值。每个提供商可以有不同的 requestTimeout
值进行精细自定义。
curl 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}' \ --header 'Content-Type: application/json' \ --data '[ { "provider": "workers-ai", "endpoint": "@cf/meta/llama-3.1-8b-instruct", "headers": { "Authorization": "Bearer {cloudflare_token}", "Content-Type": "application/json" }, "config": { "requestTimeout": 1000 }, "query": {34 collapsed lines
"messages": [ { "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "What is Cloudflare?" } ] } }, { "provider": "workers-ai", "endpoint": "@cf/meta/llama-3.1-8b-instruct-fast", "headers": { "Authorization": "Bearer {cloudflare_token}", "Content-Type": "application/json" }, "query": { "messages": [ { "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "What is Cloudflare?" } ] }, "config": { "requestTimeout": 3000 }, }]'
如果在提供商请求上设置,请求超时指定请求的超时持续时间,如果超过则返回错误。
对于提供商特定端点,通过添加 cf-aig-request-timeout
标头来配置超时值。
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/@cf/meta/llama-3.1-8b-instruct \ --header 'Authorization: Bearer {cf_api_token}' \ --header 'Content-Type: application/json' \ --header 'cf-aig-request-timeout: 5000' --data '{"prompt": "What is Cloudflare?"}'
AI 网关还支持对失败请求的自动重试,最多五次重试尝试。
此功能提高了应用的弹性,确保您可以从临时问题中恢复,而无需手动干预。
请求超时可以在通用端点上设置,也可以直接在对任何提供商的请求上设置。
使用请求重试,您可以调整三个属性的组合:
- 尝试次数(最多 5 次尝试)
- 重试前等待时间(以毫秒为单位,最多 5 秒)
- 退避方法(常量、线性或指数)
在最后一次重试尝试时,您的网关将等待直到请求完成,无论需要多长时间。
如果在通用端点上设置,请求重试将在触发任何配置的回退之前自动重试失败的请求最多五次。
对于通用端点,在提供商特定的 config
中使用以下属性配置重试设置:
config:{ maxAttempts?: number; retryDelay?: number; backoff?: "constant" | "linear" | "exponential";}
与请求超时一样,每个提供商可以有不同的重试设置进行精细自定义。
curl 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}' \ --header 'Content-Type: application/json' \ --data '[ { "provider": "workers-ai", "endpoint": "@cf/meta/llama-3.1-8b-instruct", "headers": { "Authorization": "Bearer {cloudflare_token}", "Content-Type": "application/json" }, "config": { "maxAttempts": 2, "retryDelay": 1000, "backoff": "constant" },39 collapsed lines
"query": { "messages": [ { "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "What is Cloudflare?" } ] } }, { "provider": "workers-ai", "endpoint": "@cf/meta/llama-3.1-8b-instruct-fast", "headers": { "Authorization": "Bearer {cloudflare_token}", "Content-Type": "application/json" }, "query": { "messages": [ { "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "What is Cloudflare?" } ] }, "config": { "maxAttempts": 4, "retryDelay": 1000, "backoff": "exponential" }, }]'
如果在提供商请求上设置,请求重试将自动重试失败的请求最多五次。在最后一次重试尝试时,您的网关将等待直到请求完成,无论需要多长时间。
For a provider-specific endpoint, configure the retry settings by adding different header values:
cf-aig-max-attempts
(number)cf-aig-retry-delay
(number)cf-aig-backoff
("constant" | "linear" | "exponential)
- @2025 Cloudflare Ubitools
- Cf Repo