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
* 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]>
Copy file name to clipboardExpand all lines: src/content/docs/agents/examples/manage-and-sync-state.mdx
+56-2Lines changed: 56 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Every Agent has built-in state management capabilities, including built-in stora
14
14
* Immediately consistent within the Agent: read your own writes.
15
15
* Thread-safe for concurrent updates
16
16
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`.
18
18
19
19
#### State API
20
20
@@ -194,4 +194,58 @@ 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.';
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.
0 commit comments