Skip to content

Commit f581bf5

Browse files
elithrarOxyjun
andauthored
agents: another example (#20264)
* agents: another example * Update src/content/docs/agents/examples/manage-and-sync-state.mdx * Update manage-and-sync-state.mdx * Apply suggestions from code review Co-authored-by: Jun Lee <[email protected]> --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 1c4539e commit f581bf5

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

src/content/docs/agents/examples/manage-and-sync-state.mdx

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Every Agent has built-in state management capabilities, including built-in stora
1414
* Immediately consistent within the Agent: read your own writes.
1515
* Thread-safe for concurrent updates
1616

17-
Agent state is stored in SQL database that associate with each indidivual Agent instance: you can interact with it using the higher-level `this.setState` API (recommended) or by directly querying the database with `this.sql`.
17+
Agent state is stored in a SQL database that is embedded within each individual Agent instance: you can interact with it using the higher-level `this.setState` API (recommended) or by directly querying the database with `this.sql`.
1818

1919
#### State API
2020

@@ -194,4 +194,58 @@ Learn more about the zero-latency SQL storage that powers both Agents and Durabl
194194

195195
:::
196196

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+
export class ReasoningAgent extends Agent<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+
for await (const row of result) {
213+
context.push(row.entry);
214+
}
215+
216+
const client = new OpenAI({
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.';
222+
const userPrompt = `${prompt.user}\n\nUser history:\n${context.join('\n')}`;
223+
224+
try {
225+
const completion = await client.chat.completions.create({
226+
model: this.env.MODEL || 'o3-mini',
227+
messages: [
228+
{ role: 'system', content: systemPrompt },
229+
{ role: 'user', content: userPrompt },
230+
],
231+
temperature: 0.7,
232+
max_tokens: 1000,
233+
});
234+
235+
// Store the response in history
236+
this
237+
.sql`INSERT INTO history (timestamp, user, entry) VALUES (${new Date()}, ${prompt.userId}, ${completion.choices[0].message.content})`;
238+
239+
return completion.choices[0].message.content;
240+
} catch (error) {
241+
console.error('Error calling reasoning model:', error);
242+
throw error;
243+
}
244+
}
245+
}
246+
```
247+
248+
</TypeScriptExample>
249+
250+
This works because each instance of an Agent has its _own_ database, the state stored in that database is private to that Agent: whether it's acting on behalf of a single user, a room or channel, or a deep research tool. By default, you don't have to manage contention or reach out over the network to a centralized database to retrieve and store state.
251+

0 commit comments

Comments
 (0)