Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: bedrock prompt caching (#37140)

Greg Richardson283826cbd9c0
diff --git a/apps/studio/package.json b/apps/studio/package.json
index 89abbca8108..1a32863c0af 100644
--- a/apps/studio/package.json
+++ b/apps/studio/package.json
@@ -25,7 +25,7 @@
"build:graphql-types:watch": "pnpm graphql-codegen --config scripts/codegen.ts --watch"
},
"dependencies": {
- "@ai-sdk/amazon-bedrock": "^2.2.9",
+ "@ai-sdk/amazon-bedrock": "^2.2.10",
"@ai-sdk/openai": "^1.3.22",
"@ai-sdk/react": "^1.2.12",
"@aws-sdk/credential-providers": "^3.804.0",
diff --git a/apps/studio/pages/api/ai/sql/generate-v4.ts b/apps/studio/pages/api/ai/sql/generate-v4.ts
index 58d8422da26..3792472027c 100644
--- a/apps/studio/pages/api/ai/sql/generate-v4.ts
+++ b/apps/studio/pages/api/ai/sql/generate-v4.ts
@@ -1,5 +1,5 @@
import pgMeta from '@supabase/pg-meta'
-import { streamText, tool, ToolSet } from 'ai'
+import { convertToCoreMessages, CoreMessage, streamText, tool, ToolSet } from 'ai'
import { source } from 'common-tags'
import { NextApiRequest, NextApiResponse } from 'next'
import { z } from 'zod'
@@ -435,8 +435,8 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
...filteredLocalTools,
}
+ // Important: do not use dynamic content in the system prompt or Bedrock will not cache it
const system = source`
- The current project is ${projectRef}.
You are a Supabase Postgres expert. Your goal is to generate SQL or Edge Function code based on user requests, using specific tools for rendering.
# Response Style:
@@ -581,16 +581,34 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
\`\`\`
# General Instructions:
-
- - **Available Schemas**: ${schemasString}
- **Understand Context**: Attempt to use \`list_tables\`, \`list_extensions\` first. If they are not available or return a privacy/permission error, state this and proceed with caution, relying on the user's description and general knowledge.
`
+ // Note: these must be of type `CoreMessage` to prevent AI SDK from stripping `providerOptions`
+ // https://github.com/vercel/ai/blob/81ef2511311e8af34d75e37fc8204a82e775e8c3/packages/ai/core/prompt/standardize-prompt.ts#L83-L88
+ const coreMessages: CoreMessage[] = [
+ {
+ role: 'system',
+ content: system,
+ providerOptions: {
+ bedrock: {
+ // Always cache the system prompt (must not contain dynamic content)
+ cachePoint: { type: 'default' },
+ },
+ },
+ },
+ {
+ role: 'assistant',
+ // Add any dynamic context here
+ content: `The user's current project is ${projectRef}. Their available schemas are: ${schemasString}`,
+ },
+ ...convertToCoreMessages(messages),
+ ]
+
const result = streamText({
model,
maxSteps: 5,
- system,
- messages,
+ messages: coreMessages,
tools,
})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 79249d7d3c7..8ed6b1f02c0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -189,7 +189,7 @@ importers:
next-contentlayer2:
specifier: 0.4.6
next-themes:
specifier: ^0.3.0
@@ -331,7 +331,7 @@ importers:
'@sentry/nextjs':
specifier: ^9.15.0
'@supabase/supabase-js':
specifier: 'catalog:'
version: 2.49.3
@@ -451,7 +451,7 @@ importers:
nuqs:
specifier: ^1.19.1
openai:
specifier: ^4.20.1
@@ -652,7 +652,7 @@ importers:
apps/studio:
dependencies:
'@ai-sdk/amazon-bedrock':
- specifier: ^2.2.9
+ specifier: ^2.2.10
version: 2.2.10([email protected])
'@ai-sdk/openai':
specifier: ^1.3.22
@@ -1137,7 +1137,7 @@ importers:
version: 2.4.11([email protected])
next-router-mock:
specifier: ^0.9.13
postcss:
specifier: ^8.5.3
version: 8.5.3
@@ -1308,7 +1308,7 @@ importers:
next-contentlayer2:
specifier: 0.4.6
next-themes:
specifier: ^0.3.0
@@ -2395,7 +2395,7 @@ importers:
version: link:../api-types
next-router-mock:
specifier: ^0.9.13
tsx:
specifier: ^4.19.3
version: 4.19.3
@@ -26071,7 +26071,7 @@ snapshots:
- supports-color
- webpack
dependencies:
'@opentelemetry/api': 1.9.0
'@opentelemetry/semantic-conventions': 1.32.0
@@ -34974,7 +34974,7 @@ snapshots:
dependencies:
'@contentlayer2/utils': 0.4.3
@@ -35017,7 +35017,7 @@ snapshots:
dependencies:
js-yaml-loader: 1.2.2
dependencies:
react: 18.3.1
@@ -35354,7 +35354,7 @@ snapshots:
dependencies:
mitt: 3.0.1