diff --git a/.changeset/wild-kings-appear.md b/.changeset/wild-kings-appear.md new file mode 100644 index 000000000000..07ffb54701d6 --- /dev/null +++ b/.changeset/wild-kings-appear.md @@ -0,0 +1,5 @@ +--- +"@ai-sdk/gateway": patch +--- + +Add caching property to GatewayProviderOptions type diff --git a/content/providers/01-ai-sdk-providers/00-ai-gateway.mdx b/content/providers/01-ai-sdk-providers/00-ai-gateway.mdx index 005821176249..7a007b5e9325 100644 --- a/content/providers/01-ai-sdk-providers/00-ai-gateway.mdx +++ b/content/providers/01-ai-sdk-providers/00-ai-gateway.mdx @@ -836,6 +836,12 @@ The following gateway provider options are available: The unique identifier for the entity against which quota is tracked. Used for quota management and enforcement purposes. +- **caching** _string_ + + Optional automatic caching configuration for the AI Gateway. Setting this to `'auto'` enables automatic caching of responses to optimize cost and latency. + + Example: `caching: 'auto'` + - **providerTimeouts** _object_ Per-provider timeouts for BYOK credentials in milliseconds. Controls how long to wait for a provider to start responding before falling back to the next available provider. @@ -963,6 +969,25 @@ const { text } = await generateText({ }); ``` +#### Caching Example + +Set `caching` to `'auto'` to enable automatic caching of responses, which can reduce cost and latency for repeated requests. + +```ts +import type { GatewayProviderOptions } from '@ai-sdk/gateway'; +import { generateText } from 'ai'; + +const { text } = await generateText({ + model: 'anthropic/claude-sonnet-4.6', + prompt: 'Summarize this report...', + providerOptions: { + gateway: { + caching: 'auto', + } satisfies GatewayProviderOptions, + }, +}); +``` + ### Provider-Specific Options When using provider-specific options through AI Gateway, use the actual provider name (e.g. `anthropic`, `openai`, not `gateway`) as the key: diff --git a/packages/gateway/src/gateway-provider-options.test-d.ts b/packages/gateway/src/gateway-provider-options.test-d.ts new file mode 100644 index 000000000000..c7baac4ccfd4 --- /dev/null +++ b/packages/gateway/src/gateway-provider-options.test-d.ts @@ -0,0 +1,10 @@ +import type { GatewayProviderOptions } from './gateway-provider-options'; +import { describe, expectTypeOf, it } from 'vitest'; + +describe('GatewayProviderOptions', () => { + it('caching accepts string', () => { + expectTypeOf().toEqualTypeOf< + string | undefined + >(); + }); +}); diff --git a/packages/gateway/src/gateway-provider-options.ts b/packages/gateway/src/gateway-provider-options.ts index 25ef891a80a4..fb32d6c7bc54 100644 --- a/packages/gateway/src/gateway-provider-options.ts +++ b/packages/gateway/src/gateway-provider-options.ts @@ -92,6 +92,11 @@ const gatewayProviderOptions = lazySchema(() => * Used for quota management and enforcement purposes. */ quotaEntityId: z.string().optional(), + /** + * Automatic caching configuration for the AI Gateway. + * Example: `'auto'` + */ + caching: z.string().optional(), /** * Per-provider timeouts for BYOK credentials in milliseconds. * Controls how long to wait for a provider to start responding