examples/ directory.
Each snippet assumes an ES module (top-level await), a client, and an
AGENT_ID:
npx tsx examples/01-stream-events.ts.
New to the SDK? Read Overview first for install, auth, and
the core concepts (client, resources, runs, capabilities).
Stream progress events
Watch the agent work (tool calls, LLM steps, the result) as it happens. Iterating a run yields progress events only. The finished reply is captured for you.isKnownEvent narrows the type, so event.data is fully typed in each branch.
This is progress streaming, not token streaming: the reply arrives whole, in the
result event and in the final message.
▶ examples/01-stream-events.ts
Fire and await the answer
No event handling. Just the final reply.examples/02-await-result.ts
Continue the conversation
chat.send posts a follow-up in the same thread. The agent keeps context.
examples/03-follow-up.ts
Answer questions from the agent
When the agent needs input, it ends its turn withquestions (a prompt plus
options) instead of a final answer. Answer with a normal follow-up.
examples/04-answer-questions.ts
Cap the tool rounds
maxSteps limits how many tool rounds the agent runs for one message. When
the cap is reached, the agent recaps and asks whether to continue. The message
still ends with status: 'success'. The cap applies to that message only, so
pass it again on a follow-up. Sub-agents have their own counter. See
Limiting agent steps.
examples/07-max-steps.ts
Stop a message
chat.cancel stops an assistant message that is still being generated. It
returns the message with status set to 'cancelled'. It is not idempotent:
cancelling a message that already finished throws APIError (400). A “stop”
button should treat that error as “too late”.
AbortSignal is
different: it only closes your connection, and the agent keeps running.
Export a message as a document
chat.export renders a finished message to a PDF or a DOCX and returns the
bytes as a Uint8Array. The server sends no file name, so pick one when you
write the file.
APIError (400). Any format other than 'pdf' or 'docx' throws
InvalidParams before a request is sent.
Give the agent files
chat.uploadFile registers a file. Its id goes in fileIds on chat.start
or chat.send, at most 5 files per message. Pass a File (its name and type
are used), or a Blob, ArrayBuffer, or Uint8Array with a filename. In
Node, open a path with openAsBlob. A string is rejected: it is ambiguous
between a path and text.
InvalidParams before anything
is sent. A download is held in memory. See
the reference for the limits and
errors.
List and search threads
examples/05-list-threads.ts
Manage a thread
examples/06-manage-thread.ts
Store and recall a memory file
client.memory stores files in a namespace you choose. The same API key shares
those files across userId values. update is partial, and its required
version guards against overwriting a concurrent change. A stale one throws
ConflictError. To use the files in a conversation, pass a memory capability
in threadCapabilities to chat.start. The concepts are in
Memory namespaces.
examples/08-memory-files.ts
Control what the agent can use
Capabilities decide which tools the agent can use: web search, document search, MCP connections, skills, media generation, and memory. Set them for a whole thread withthreadCapabilities (on chat.start only), or for one message with
messageScope (on chat.start and chat.send).
Use a custom managed agent
A custom agent (its own model, failover order, and instructions, configured at platform.cominty.ai → Agents) is just anotheragentId. Pass it to any chat call. This example feeds a jargon-heavy
incident report to an agent instructed to reply with a clean, non-technical
French executive summary.
Pull live context from an MCP server
MCP servers connected to an agent on the platform (for example Linear) work with no SDK change. Capabilities are on by default, and a message scope turns off what you don’t want. Here the custom agent reads the current Linear sprint and reports it in its French C-level voice. Linear tool calls appear in the stream astool_call events.
List the MCP integrations
mcp.list returns the integrations your API key can use. A server’s name is
the value mcp.connections stores. By default it keeps only the servers that
are turned on.
requires_oauth_connect is true when someone must connect the integration on
the platform first. If the manifest cannot be loaded, the SDK throws
ServerError.
▶ examples/12-mcp-servers.ts
Create a custom agent
Create an agent from code, with its own instructions and capabilities. The agent is owned by the user who created your API key. Chat with it like any other agent.update is partial: null clears description, instructions, and
capabilities, and a capabilities object is merged capability by capability.
An agent’s capabilities come back in the same camelCase shape you pass, so a
read can go straight back into update. See
Custom agents.
▶ examples/10-agents.ts
Choose the agent’s models
agents.listModels gives the model ids. setModels replaces the agent’s
chain: the first model answers, and the next ones take over when it fails.
setModels changes the chain of the whole organization.
Give an agent a skill
A skill is a folder with aSKILL.md. Pass its files by path and
skills.create zips them in memory, or pass a zip as is. Give the skill to an
agent with the skills capability.
readdir. See
Skills for the folder layout and the versions.
▶ examples/11-skills.ts
Search your own files
sources.create turns files into an indexed source in one call. Wait for it to
be synced, then give its id to an agent. The chat must run with the key
owner’s userId, which is agent.owner.
source.pid (a string) goes to every client.sources method. source.id (a
number) goes to sourceIds. waitSynced takes milliseconds. See
Sources.
▶ examples/09-sources.ts
Resolve a citation
An answer cites documents as<cite document_id="2698#10778" .../>.
documents.get resolves that id to the source and the file.
NotFoundError. There is no call that lists documents.
Handle errors
chat.start succeeded, so wrap the
code that reads the run too. The SDK never retries on its own: sending a message
twice would start a second run. See Errors for the
full hierarchy.