connectAgent()
Registers a handler as a simulation target. The SDK opens one outbound connection per process, registers the agent with its name, its environment and its declared parameters, and calls the handler once per conversation turn.
zod declares the run parameters, so a project installs langwatch zod. connectAgent returns the handler, so supportAgent({ messages }) calls it directly and a unit test needs no connection. The returned function also exposes .disconnect(), which closes the connection and drops the agent from the presence list.
The process connects only with an API key. The SDK reads LANGWATCH_API_KEY from the environment, logs one line when it is absent, and lets the process run.
ConnectAgentOptions
Environment resolution
environment, then LANGWATCH_AGENT_ENVIRONMENT, then APP_ENV, ENVIRONMENT and NODE_ENV, then development.
development makes the agent personal: it belongs to the API key’s owner when the key is personal, and to the machine when the key is a project key. Every other name is shared by the project. See Environments and personal agents.
The handler argument
The handler receives one object.parameters
Three forms, all of which reach the platform as the same declaration.
A zod schema, the form the example above uses. params is typed from it:
Give every property a default, or the run must supply a value for it. Keep the schema flat and scalar: nested objects and arrays are not run parameters.
Pass the schema object itself, never
schema["~standard"].jsonSchema and never a validator instance. The SDK reads the JSON Schema converter the object carries under "~standard", so valibot, arktype and any other Standard Schema library work the same way, and the SDK imports none of them.
A definition map, for a project with no schema library:
A plain JSON Schema object,
{ type: "object", properties: { ... } }.
Values arrive validated against the declaration. A value the declaration refuses fails the call with agent_parameter_invalid before the handler runs. A parameter the run did not supply arrives as its default; one with no default that the run did not supply fails the call with the name in the message.
Return values
session is an opaque JSON value the platform echoes on the next turn of the same conversation, up to 64 kilobytes.
Lifecycle
The connection starts on the next tick after the firstconnectAgent call, once per process, and one connection carries every agent the process declares. It uses the global WebSocket when the runtime has one and the ws package otherwise. While connected, it keeps the event loop alive, so node agent.ts serves until Ctrl-C.
SIGINT, SIGTERM and normal exit send a deregistration, so the agent reads Offline at once. A dropped connection is retried with a growing wait, from 1 second to 30 seconds.
Transport
The connection is an outbound WebSocket by default. On a network that blocks WebSockets, the same frames travel over HTTP long polling: the SDK posts the registration, waits on a request for the next turn, and posts each answer, through the globalfetch of Node 20 or later. Pass transport: "http" or set LANGWATCH_AGENT_TRANSPORT=http to use it from the start. With the default transport, a proxy that answers the WebSocket upgrade with an HTTP status makes the SDK switch to HTTP on its own, with one warning line that names the status. The ws package is what reports that status; the global WebSocket cannot, so on a runtime without ws set the transport explicitly.
Environment variables
Errors
A failing call answers with a typed code rather than a stack trace.agent_offline, agent_owner_only, agent_call_timeout, agent_call_failed, agent_disconnected, agent_instance_lost, agent_busy, agent_parameter_invalid, agent_register_refused, agent_payload_too_large.
Next steps
Connect your agent
The three-step guide, with the session and parameter recipes
TypeScript SDK reference
Setup, tracing, spans and prompt management