> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Connectors and Agents

> Call a language model from LoT with a TYPE LLM connector, or run a tool-using assistant with DEFINE AGENT on that connector

## A model call, or an assistant that can act

Your broker already moves sensor data, drives equipment, and stores history. Two LoT pieces add a language model to that world.

A **`TYPE LLM` connector** sends one prompt and returns text. Actions use it for a classification, a summary, or a short conversation. A **`DEFINE AGENT`** is a named assistant that runs on that connector. It can read topics, publish, run connector events, and hand work to other agents — only where you grant it.

The connector holds the provider, the model, the key, and the price. Every agent that names it with `USES LLM CONNECTOR` picks up a change to that connector. An agent is its own LoT entity. It is deployed with `-addAgent`, and it does not appear in the connector list.

> [!TIP]
>
> **Like a phone line and the people who answer it.** The `TYPE LLM` connector is the line to the model. Each `DEFINE AGENT` is a person you hire, with a job description and a set of keys. `CALL LLM` dials the line directly. `CALL AGENT` asks a specific person to do the job.

### When to use which

* **Prompt in, text out**: a `TYPE LLM` connector and `CALL LLM` inside an Action. This page.
* **An assistant that reads or changes the broker**: a `DEFINE AGENT` on that connector. See [Agents Overview](https://docs.coreflux.org/v2.22/lot-language/agents/overview.md).

Both need a **Growth or Enterprise** license, the same row as **LLM / Agent** on [Licensing](https://docs.coreflux.org/v2.22/mqtt-broker/licensing.md#plans).

> [!NOTE]
>
> First time? Paste a working agent in [Creating Agents in Coreflux](https://docs.coreflux.org/v2.22/quick-start/creating-agents.md). Come back here for providers and `CALL LLM`. Grants, topics, and the move off `TYPE AGENT` are in [Agent Syntax](https://docs.coreflux.org/v2.22/lot-language/agents/syntax.md).

***

The example below is complete. Deploy the LLM connector with `-addConnector`.

## Quick Start

> [tab] Ask a model from an Action
>
> One prompt, one answer. The connector has no tools.
>
> Store the key once with `KEEP SECRET "OPENAI_API_KEY" WITH "sk-..."`, or with `-setSecret`. See [Secrets](https://docs.coreflux.org/v2.22/mqtt-broker/secrets-and-env.md).
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR PlantModel WITH TYPE LLM
>     ADD LLM_CONFIG
>         WITH PROVIDER "openai"
>         WITH MODEL "gpt-6-luna"
>         WITH API_KEY GET SECRET "OPENAI_API_KEY"
>         WITH API_SURFACE "auto"
>
> DEFINE ACTION ExplainAlarm
> ON TOPIC "alarms/raw/+" DO
>     CALL LLM "PlantModel.generate"
>         WITH (prompt = PAYLOAD)
>         RETURN AS {result}
>     PUBLISH TOPIC "alarms/explained" WITH (GET JSON "response" IN {result} AS STRING)
> ```
>
> Publish an alarm payload to `alarms/raw/line3`. The explanation arrives on `alarms/explained`. Read `success` in `{result}` before you trust `response`.

For agent examples, see [Agent Examples](https://docs.coreflux.org/v2.22/lot-language/agents/examples.md).

***

## The LLM connector

`DEFINE CONNECTOR … WITH TYPE LLM` is the connector (`DEFINE ROUTE` and the `-addRoute` / `-removeRoute` commands still work as aliases). Actions call it with `CALL LLM`. Agents name it with `USES LLM CONNECTOR`. The connector itself never calls tools.

### Picking a provider

| Provider | `WITH PROVIDER` | Typical `BASE_URL` | Auth |
| - | - | - | - |
| **Ollama** (default) | `ollama` | `http://localhost:11434` | none |
| **OpenAI** | `openai` | omit (the provider default) | `API_KEY` |
| **Anthropic** | `anthropic` | omit (the provider default), or a proxy | `API_KEY` |
| **OpenAI-compatible** | `compatible` | your gateway URL | `API_KEY` when that server requires one |

> [!NOTE]
>
> **Local Ollama.** [Install Ollama](https://ollama.com/download) on the broker host, or on a machine the broker can reach, and pull a **tools-capable** model before you deploy (`ollama pull llama3.2` or `ollama pull qwen3`). The `MODEL` value must match `ollama list`. Only some Ollama models call tools. Coreflux connects to the endpoint you set. It does not install Ollama or download models.
>
> If the broker runs in **Docker**, `localhost` inside the container is the container. Point `BASE_URL` at the Ollama service on the Compose network (for example `http://ollama:11434`), or at `http://host.docker.internal:11434` when Ollama runs on the Docker host.

> [tab] Ollama
>
> The default provider. Omit `PROVIDER` and the connector still uses Ollama at `http://localhost:11434`.
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR LocalLlama WITH TYPE LLM
>     ADD LLM_CONFIG
>         WITH PROVIDER "ollama"
>         WITH BASE_URL "http://localhost:11434"
>         WITH MODEL "llama3.2"
> ```

> [tab] OpenAI
>
> `API_SURFACE "auto"` uses the Responses API, which current reasoning models such as `gpt-6-luna` require. Leave `TEMPERATURE` unset for those models.
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR Gpt6 WITH TYPE LLM
>     ADD LLM_CONFIG
>         WITH PROVIDER "openai"
>         WITH MODEL "gpt-6-luna"
>         WITH API_KEY GET SECRET "OPENAI_API_KEY"
>         WITH API_SURFACE "auto"
>         WITH REASONING_EFFORT "medium"
>         WITH PRICE_INPUT "1.25"
>         WITH PRICE_OUTPUT "10"
> ```

> [tab] Anthropic
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR Claude WITH TYPE LLM
>     ADD LLM_CONFIG
>         WITH PROVIDER "anthropic"
>         WITH MODEL "claude-sonnet-5-5"
>         WITH API_KEY GET SECRET "ANTHROPIC_API_KEY"
> ```

> [tab] OpenAI-compatible
>
> Use `compatible` for a gateway that speaks the OpenAI HTTP API. `API_SURFACE "auto"` on this provider uses Chat Completions. Set `API_SURFACE "responses"` only when that gateway implements the Responses API.
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR Gateway WITH TYPE LLM
>     ADD LLM_CONFIG
>         WITH PROVIDER "compatible"
>         WITH BASE_URL "https://llm.example.com/v1"
>         WITH MODEL "local-large"
>         WITH API_KEY GET SECRET "GATEWAY_API_KEY"
>         WITH API_SURFACE "chat_completions"
> ```

### `LLM_CONFIG`

| Parameter | Required | Default | Description |
| - | - | - | - |
| `MODEL` | **Yes** | — | Model name the provider expects (`llama3.2`, `gpt-6-luna`, `claude-sonnet-5-5`) |
| `PROVIDER` | No | `ollama` | `ollama`, `openai`, `anthropic`, or `compatible` |
| `BASE_URL` | No | Ollama: `http://localhost:11434`. Others: the provider default | Required in practice for `compatible` |
| `API_KEY` | Conditional | — | Required for `openai`, `anthropic`, and most `compatible` endpoints. Use `GET SECRET` |
| `API_SURFACE` | No | `auto` | `openai` and `compatible` only: `auto`, `responses`, or `chat_completions`. `auto` is Responses for `openai` and Chat Completions for `compatible` |
| `REASONING_EFFORT` | No | see below | `none`, `low`, `medium`, `high`, or `extra_high`, sent on every call. `none` sends nothing |
| `TEMPERATURE` | No | Ollama: `0.7`. Others: not sent | `0.0`–`2.0`. Leave it unset for reasoning models, which reject a temperature |
| `MAX_TOKENS` | No | `2048` | Maximum completion tokens for **one** call. An agent's `MAX_TOKENS` is a different budget |
| `PRICE_INPUT` | No | — | Price per million input tokens. Agent tasks then report `cost` on `run_end` when either price is set |
| `PRICE_OUTPUT` | No | — | Price per million output tokens |
| `SYSTEM_PROMPT` | No | empty | Default system message for `CALL LLM`. Agents use their own `SYSTEM_PROMPT` |

> [!NOTE]
>
> **Models that reason before answering** spend part of the connector's `MAX_TOKENS` on reasoning they don't show. With `1024` the answer can come back empty. Set `MAX_TOKENS` to `4096` or more on the `TYPE LLM` connector for these models.

Without `REASONING_EFFORT`, only `PROVIDER "openai"` on the Responses API sends one: Medium. That default is skipped for older families (`gpt-4*`, `gpt-3*`, `chatgpt-*`), which reject it. A `compatible` connector sends no reasoning effort unless you set it. Set `REASONING_EFFORT "none"` when an OpenAI model rejects the default.

`MCP_ROUTES` is legacy. On a `TYPE LLM` connector the broker ignores it and logs a warning to use `ADD TOOL`. The connector still starts. A `TYPE LLM` call runs no tools. On `DEFINE AGENT` it is a legacy clause, and `-addAgent` refuses it. To give an agent an MCP tool, see [Agents and MCP Tools](https://docs.coreflux.org/v2.22/lot-language/routes/mcp-routes.md#agents-and-mcp-tools). See [Coming from TYPE AGENT](https://docs.coreflux.org/v2.22/lot-language/agents/overview.md#coming-from-type-agent).

The connector does not probe the provider at start. A wrong key, URL, or model shows up on the first call, as `"success": false`.

### Ollama context and timeouts

Ollama uses the model's default context window (often 2k–8k tokens) unless the request asks for more, and silently drops the start of a longer prompt. The broker therefore sends `num_ctx` on every Ollama call, for `CALL LLM` and for each step of an agent's tool loop:

* It estimates the prompt (messages, tool results, and tool schemas) plus `MAX_TOKENS`.
* It uses 16,384 tokens when that fits, otherwise `MAX_CONTEXT_WINDOW`. Only two sizes, because each change makes Ollama reload the model.
* It never goes above the model's own maximum, read from Ollama and cached for five minutes.
* A prompt that still doesn't fit fails with `context_overflow` and is never sent.

| Parameter | Default | Description |
| - | - | - |
| `CONTEXT_WINDOW` | sized per call | A fixed `num_ctx` in tokens instead of the automatic size. Still capped at the model's maximum |
| `MAX_CONTEXT_WINDOW` | `32768` | The largest `num_ctx` the broker picks. Larger windows use more memory on the Ollama host |
| `FIRST_OUTPUT_TIMEOUT` | `120` | Seconds from sending a request to its first streamed output, model load included. `0` turns it off |
| `IDLE_TIMEOUT` | `60` | Longest gap, in seconds, between streamed chunks. `0` turns it off. Time spent in tools never counts |

These four settings apply to `PROVIDER "ollama"` only.

```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR LocalLlama WITH TYPE LLM
    ADD LLM_CONFIG
        WITH PROVIDER "ollama"
        WITH BASE_URL "http://ollama:11434"
        WITH MODEL "qwen3"
        WITH MAX_CONTEXT_WINDOW 16384
        WITH FIRST_OUTPUT_TIMEOUT 300
```

Both timeouts apply per gap, so a long answer that keeps arriving never times out. An agent's `TASK_TIMEOUT` and cancel also end a call that is waiting on Ollama. A large model loading cold on a slow machine can take more than two minutes: raise `FIRST_OUTPUT_TIMEOUT` for it. On a low-memory Ollama host, a 32k window may not fit next to the model. Lower `MAX_CONTEXT_WINDOW` (for example to `16384`).

| Failure code | Meaning |
| - | - |
| `llm_timeout` | No output within `FIRST_OUTPUT_TIMEOUT`, or a gap longer than `IDLE_TIMEOUT` |
| `llm_unreachable` | Nothing listens at `BASE_URL`: Ollama isn't running, or the host or port is wrong |
| `model_not_found` | Ollama doesn't have `MODEL`. Run `ollama pull` or fix the name |
| `context_overflow` | The prompt doesn't fit the largest window the connector allows |

Agents report these as the task's `code`. `CALL LLM` returns `"success": false` with the same message, which names the model and the URL, in `error`.

### Call the model from an Action

| Method | What it does |
| - | - |
| `generate` | One turn. No history |
| `chat` | Multi-turn. Pass `use_history = "true"` and a `session_id` to keep the thread |
| `clear_history` | Drops the in-memory thread for that `session_id` |

`CALL LLM` and `INVOKE LLM` are the same call.

```lot wrap focus={7-13} theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR PlantModel WITH TYPE LLM
    ADD LLM_CONFIG
        WITH PROVIDER "openai"
        WITH MODEL "gpt-6-luna"
        WITH API_KEY GET SECRET "OPENAI_API_KEY"

DEFINE ACTION ContinueThread
ON TOPIC "assistants/+/message" DO
    SET "user_id" WITH TOPIC POSITION 2
    CALL LLM "PlantModel.chat"
        WITH (prompt = PAYLOAD, use_history = "true", session_id = {user_id})
        RETURN AS {result}
    PUBLISH TOPIC "assistants/" + {user_id} + "/reply" WITH (GET JSON "response" IN {result} AS STRING)
```

A success payload for `generate` and `chat` includes `success`, `response`, `model`, `session_id`, `used_history`, and `execution_time_ms`. A failure sets `success` to `false` and puts the reason in `error`.

History for `CALL LLM` stays in memory on that connector: the last 50 messages per `session_id`, dropped when the connector stops or the broker restarts. Agent conversations are separate and survive a restart. See [Working memory](https://docs.coreflux.org/v2.22/lot-language/agents/syntax.md#working-memory).

You can also fire the connector with `TRIGGER "PlantModel" TO {prompt} FOR "generate"` (or `FOR "chat"` for the history mapping). That path publishes the result to the mapping's `DESTINATION_TOPIC`, or to `{RouteName}/llm/response` when that mapping has none. `TRIGGER` keeps chat history only when the mapping name is `chat`, unless the JSON payload sets `use_history`.

Status is the usual connector topic `$SYS/Coreflux/Routes/{Name}/status`. The connector does not publish `…/Capabilities` or `…/Metrics`.

`-discoverAIProviders` probes local and cloud model lists and retains a summary on `$SYS/Coreflux/AI/Providers`. That probe is independent of the providers a `TYPE LLM` connector accepts.

***

## Agents

`DEFINE AGENT` has its own section: [Agents Overview](https://docs.coreflux.org/v2.22/lot-language/agents/overview.md), [Agent Syntax](https://docs.coreflux.org/v2.22/lot-language/agents/syntax.md) (properties, `ADD TOOL`, delegation, topics, `CALL AGENT`, management), and [Agent Examples](https://docs.coreflux.org/v2.22/lot-language/agents/examples.md). Moving off `TYPE AGENT`? See [Coming from TYPE AGENT](https://docs.coreflux.org/v2.22/lot-language/agents/overview.md#coming-from-type-agent).

***

## Troubleshooting

> [accordion] Ollama never calls a tool
>
> The model does not support tools. Pull a tools-capable tag such as `llama3.2` or `qwen3`.

> [accordion] OpenAI returns HTTP 400
>
> Reasoning models reject `TEMPERATURE`. Leave it unset. `gpt-6-luna` rejects a Chat Completions call. Use `API_SURFACE "auto"` or `"responses"`.

***

***

## Limitations

> [!WARNING]
>
> `CALL LLM` waits for the full answer and does not stream. `CALL LLM` history is in memory only. For agent limits, see [Agents Overview](https://docs.coreflux.org/v2.22/lot-language/agents/overview.md#limits).

***

## Next steps

> [card] [Creating Agents in Coreflux](https://docs.coreflux.org/v2.22/quick-start/creating-agents.md)
>
> Paste a site guide, a scheduled report, alert triage, or a shift handover and try them over MQTT.

> [card] [MCP Connectors](https://docs.coreflux.org/v2.22/lot-language/routes/mcp-routes.md)
>
> Call external MCP tools from Actions. Agents do not receive those tools automatically.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.