Chat
DocsChat is a repeated model call with memory. This demo stores recent turns in key-value storage, then includes that history on the next request so the model can answer with context. Use this shape for lightweight chat history. Use State and Memory when the state needs stronger ownership, search, or retention rules.
Loading conversation
Reference Code
import { KeyValueClient } from "@agentuity/keyvalue";
import { AIGatewayClient } from "@agentuity/aigateway";
type Message = {
role: "user" | "assistant";
content: string;
};
const kv = new KeyValueClient();
const gateway = new AIGatewayClient();
const MODEL = "anthropic/claude-opus-4-8";
async function chat(conversationId: string, message: string) {
const key = "conversation:" + conversationId + ":messages";
const history = await kv.get<Message[]>("chat-history", key);
const messages = history.exists ? history.data : [];
const result = await gateway.completeText({
model: MODEL,
messages: [
{ role: "system", content: "You are a concise support assistant." },
...messages,
{ role: "user", content: message },
],
});
if (!result.hasText) {
throw new Error("The model returned no text.");
}
const nextMessages = [
...messages,
{ role: "user", content: message },
{ role: "assistant", content: result.text },
].slice(-50);
await kv.set("chat-history", key, nextMessages, { ttl: 60 * 60 });
return {
reply: result.text,
turns: nextMessages.length / 2,
conversationId,
};
}
export const preview = await chat(
"conv-" + crypto.randomUUID(),
"What is Agentuity?"
);Ready
Output will appear here...