> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/advanced/reference/built-in-functions/aigatewaysession.md).

# aiGatewaySession

Wires one AiAgent to one-or-more IGateway instances for inbound message handling, with a configurable dispatch policy for messages arriving on a busy thread.

Wires one `AiAgent` to one-or-more `IGateway` instances for inbound message handling. Returns an unstarted `GatewaySession` — call `.start()` to begin accepting inbound messages.

## Syntax

```javascript
aiGatewaySession( agent, gateways, policy, maxQueueDepth, checkpointer )
```

## Parameters

| Parameter       | Type                              | Required | Default   | Description                                                                                                                                 |
| --------------- | --------------------------------- | -------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `agent`         | `AiAgent`                         | ✅        | —         | The agent every inbound message dispatches to                                                                                               |
| `gateways`      | `string` \| `IGateway` \| `array` | ✅        | —         | A single gateway or an array. Each entry is either a string name (resolved via `aiGateway()`) or an already-constructed `IGateway` instance |
| `policy`        | `string`                          | ❌        | `"queue"` | `"reject"` \| `"queue"` \| `"steer"` \| `"interrupt"` — how to handle an inbound message on a thread that already has a turn in flight      |
| `maxQueueDepth` | `numeric`                         | ❌        | `50`      | Cap on buffered messages per thread for the `"queue"`/`"interrupt"` policies                                                                |
| `checkpointer`  | `IAiMemory`                       | ❌        | —         | Passed to every gateway's `setCheckpointer()`                                                                                               |

## Returns

An unstarted `GatewaySession` instance.

## Methods

| Method                       | Returns          | Description                                                 |
| ---------------------------- | ---------------- | ----------------------------------------------------------- |
| `.start()`                   | `GatewaySession` | Begin accepting inbound messages on every attached gateway  |
| `.stop()`                    | `GatewaySession` | Stop accepting inbound messages                             |
| `.isRunning()`               | `boolean`        | Whether `start()` has been called and `stop()` hasn't since |
| `.getActiveThreadIds()`      | `array`          | Threads with a turn currently in flight                     |
| `.getQueueDepth( threadId )` | `numeric`        | How many messages are buffered for a thread                 |

## Examples

### Single Gateway

```javascript
session = aiGatewaySession( agent: myAgent, gateways: "cli" )
session.start()
```

### Multiple Gateways, Steer Policy

```javascript
session = aiGatewaySession(
    agent   : myAgent,
    gateways: [ "cli", aiGateway( "http", { secret: getSecret() } ) ],
    policy  : "steer"
)
session.start()

if ( session.isRunning() ) {
    writeOutput( "Active threads: #arrayLen( session.getActiveThreadIds() )#" )
}
```

## Events

Fires `onGatewaySessionCreate` with `{ session }` when constructed.

## Related

* [Gateway Sessions](/main-components/gateway-sessions.md)
* [Agent Run Control](/main-components/agents/run-control.md)
* [aiGateway()](/advanced/reference/built-in-functions/aigateway.md) · [aiGatewayRegistry()](/advanced/reference/built-in-functions/aigatewayregistry.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai.ortusbooks.com/advanced/reference/built-in-functions/aigatewaysession.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
