Skip to main content

@langwatch.connect_agent()

Registers the decorated function as a simulation target. The SDK opens one outbound connection per process, registers the agent with its name, its environment and the parameters read from the signature, and calls the function once per conversation turn.
The function keeps working as a plain function. A direct call runs the body and touches no connection. 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.

Options

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.

Turn fields

The platform sends the same fields on every call. The SDK reads the signature once, at decoration, and passes exactly the names the function declares. A function that declares **kwargs receives all of them. These names are never run parameters.

langwatch.AgentCall

A function whose first parameter is annotated langwatch.AgentCall receives one object carrying every turn field plus parameters, instead of separate arguments.

Run parameters

Every parameter that is not a turn field is a run parameter. A parameter with a default is optional, and the run dialog prefills that default. A parameter with no default is required, and the run must supply a value for it. An annotation the SDK does not map falls back to that type’s JSON schema and is offered as text. Values arrive validated against the declaration. A value the declaration refuses fails the call with agent_parameter_invalid before the function 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.

langwatch.Param()

Annotates one run parameter.

Return values

A generator is refused with a message saying that streaming is not supported.

langwatch.AgentReply

langwatch.agent.serve()

Blocks the calling thread while the connection runs, for a script whose only job is the agent. A web server does not need it: the connection runs on its own daemon thread.

Lifecycle

The connection starts on first use, once per process, on a daemon thread with its own event loop. A fork restarts it in the child, so a preloading worker server keeps working. 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. 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 transport in use is printed on the connected line.

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

Python SDK reference

Setup, tracing, spans and prompt management
Last modified on August 31, 2026