TengoChatDevelopers

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"}'

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.

JSON
{
  "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: tool with {"name": "github_list_pulls"} when a tool starts (show "Checking GitHub..." in your app)
  • event: delta with {"text": "..."} for each piece of the answer
  • event: done with the whole answer, its thread and usage
  • event: error with {"type", "message"} if something went wrong
JavaScript
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);
  }
}