Call your agent from your app
Threads, one memory per end user, streaming events and tool calls.
Your agent's id is in the builder (it starts with ag_). Send it a message; it answers, using its tools and connectors as it needs.
The first message
curl https://tengochat.com/api/v1/agents/ag_YOUR_AGENT/messages \
-H "Authorization: Bearer $TENGOCHAT_API_KEY" \
-H "Content-Type: application/json" \
-d '{"message": "Which pull requests need a review?", "user": "customer-4821"}'const res = await fetch("https://tengochat.com/api/v1/agents/ag_YOUR_AGENT/messages", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.TENGOCHAT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ message: "Which pull requests need a review?", user: "customer-4821" }),
});
const { content, thread_id, tools_used, usage } = await res.json();import os, requests
res = requests.post(
"https://tengochat.com/api/v1/agents/ag_YOUR_AGENT/messages",
headers={"Authorization": f"Bearer {os.environ['TENGOCHAT_API_KEY']}"},
json={"message": "Which pull requests need a review?", "user": "customer-4821"},
)
answer = res.json()
print(answer["content"], answer["thread_id"])Keep the conversation going
The answer carries a thread_id. Send it back with the next message and the agent remembers what was said. Store one thread per person (or per chat window) on your side.
{
"message": "And which of them touch the login page?",
"thread_id": "th_5d0c2e7a1b9f3c8d4e6a",
"user": "customer-4821"
}user is your own id for the person talking: it keeps their threads apart in your usage and lets you find them later. Read a thread with GET /threads/{id}, forget it with DELETE /threads/{id}.
Stream the answer
Add "stream": true to get events as they happen:
event: toolwith{"name": "github_list_pulls"}when a tool starts (show "Checking GitHub..." in your app)event: deltawith{"text": "..."}for each piece of the answerevent: donewith the whole answer, its thread and usageevent: errorwith{"type", "message"}if something went wrong
const res = await fetch(url, { method: "POST", headers, body: JSON.stringify({ message, thread_id, stream: true }) });
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let cut;
while ((cut = buffer.indexOf("\n\n")) !== -1) {
const block = buffer.slice(0, cut);
buffer = buffer.slice(cut + 2);
const event = /^event: (.*)$/m.exec(block)?.[1];
const data = JSON.parse(/^data: (.*)$/m.exec(block)?.[1] ?? "{}");
if (event === "delta") render(data.text);
if (event === "tool") showStatus(data.name);
if (event === "done") saveThread(data.thread_id);
}
}