You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/agents/examples/manage-and-sync-state.mdx
+52-1Lines changed: 52 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,4 +194,55 @@ Learn more about the zero-latency SQL storage that powers both Agents and Durabl
194
194
195
195
:::
196
196
197
-
The SQL API exposed to an Agent is similar to the one [within Durable Objects](/durable-objects/api/sql-storage/): Durable Object SQL methods available on `this.ctx.storage.sql`. You can use the same SQL queries with the Agent's database, create tables, and query data, just as you would with Durable Objects or [D1](/d1/).
197
+
The SQL API exposed to an Agent is similar to the one [within Durable Objects](/durable-objects/api/sql-storage/): Durable Object SQL methods available on `this.ctx.storage.sql`. You can use the same SQL queries with the Agent's database, create tables, and query data, just as you would with Durable Objects or [D1](/d1/).
198
+
199
+
### Use Agent state as model context
200
+
201
+
You can combine the state and SQL APIs in your Agent with its ability to [call AI models](/agents/examples/using-ai-models/) to include historical context within your prompts to a model. Modern Large Language Models (LLMs) often have very large context windows (up to millions of tokens), which allows you to pull relevant context into your prompt directly.
202
+
203
+
For example, you can use an Agent's built-in SQL database to pull history, query a model with it, and append to that history ahead of the next call to the model:
204
+
205
+
<TypeScriptExample>
206
+
207
+
```ts
208
+
exportclassReasoningAgentextendsAgent<Env> {
209
+
async callReasoningModel(prompt:Prompt) {
210
+
let result =this.sql<History>`SELECT * FROM history WHERE user = ${prompt.userId} ORDER BY timestamp DESC LIMIT 1000`;
211
+
let context = [];
212
+
forawait (const row ofresult) {
213
+
context.push(row.entry);
214
+
}
215
+
216
+
const client =newOpenAI({
217
+
apiKey: this.env.OPENAI_API_KEY,
218
+
});
219
+
220
+
// Combine user history with the current prompt
221
+
const systemPrompt =prompt.system||'You are a helpful assistant.';
0 commit comments