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

# LLM

> How Formal proxies LLM API traffic through the Formal Endpoint or a Connector

## Overview

LLM resources let Formal inspect and control traffic to LLM provider APIs, such as `api.anthropic.com` or `chatgpt.com`. Formal can proxy this traffic in two places:

* **Formal Endpoint**: [Transparent Mode](https://docs.formal.ai/docs/guides/client-apps/transparent-mode.md) intercepts LLM traffic on **macOS**, **Linux**, and the [Formal Endpoint for Kubernetes](https://docs.formal.ai/docs/guides/client-apps/kubernetes-egress.md). The Endpoint evaluates policies and forwards requests to the provider.
* **Connector**: A [Connector](https://docs.formal.ai/docs/glossary/index.md#connector) in your infrastructure evaluates policies and forwards requests to the provider. Traffic reaches it from the Formal Endpoint or directly from your applications.

```text theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
your app  →  Formal Endpoint  →  LLM provider API
your app  →  Formal Endpoint (optional)  →  Connector  →  LLM provider API
```

## Creating LLM Resources

You must create an LLM resource in Formal for each LLM API you want to intercept. The resource tells Formal which upstream hostname and port to forward traffic to.

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_resource" "anthropic" {
  name       = "anthropic"
  technology = "llm"
  hostname   = "api.anthropic.com"
  port       = 443
}

resource "formal_resource" "chatgpt" {
  name       = "chatgpt"
  technology = "llm"
  hostname   = "chatgpt.com"
  port       = 443
}
```

## Connecting

### Through the Formal Endpoint

LLM resources do not support `formal connect`. Instead, enable [Transparent Mode](https://docs.formal.ai/docs/guides/client-apps/transparent-mode.md) and write a [network rule](https://docs.formal.ai/docs/guides/network-rules.md) that matches LLM traffic. This rule matches known agents on macOS and Linux, and known LLM provider hostnames on Linux:

```cel theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
{
  "condition": {
    "pre_tcp": process.is_agent,
    "pre_tls": process.is_agent || (is_llm_hostname && os == "linux"),
    "post_tls": technology == "llm"
  }
}
```

To have a Connector evaluate this traffic instead of the Endpoint, set `forward_to_connector` in the rule's [outputs](https://docs.formal.ai/docs/guides/network-rules.md#rule-structure). The LLM resource must be reachable through a Connector listener.

### Through a Connector

Applications in your infrastructure can send LLM traffic to a Connector directly. Link the LLM resource to a Connector listener, then replace the provider hostname with the Connector hostname. When the listener serves several resources, prefix the hostname with the resource name, as described in [Smart Routing](https://docs.formal.ai/docs/guides/core-concepts/resources/smart-routing.md#http):

* **Before:** `https://api.anthropic.com/v1/messages`
* **After:** `https://anthropic.<connector-hostname>/v1/messages`

As with [HTTP resources](https://docs.formal.ai/docs/guides/core-concepts/resources/http.md), set the `X-Formal-User-Username` and `X-Formal-User-Password` headers to associate requests with a Formal identity.

## Observability

### Log source

Logs appear with `source: desktop` when the Formal Endpoint evaluates the traffic, and with `source: connector` when a Connector does. Filter for them in the [Logs page](https://app.formal.ai/logs) with `source:desktop` or `source:connector`.

To encrypt or drop LLM content in these logs, create a [log schema](https://docs.formal.ai/docs/guides/observability/log-schemas.md#llm-content). Use `log.source == "endpoint"` or `log.source == "connector"` in its scope to target one source.

### What is captured

Formal captures the full content of each LLM request and response, including:

* **Model and provider** — the model name extracted from the request body
* **Messages** — the full prompt and completion text
* **Tool calls** — any tool/function calls the model makes, including their names, arguments, and results
* **Token usage** — prompt tokens, completion tokens, and total tokens
* **Streaming responses** — individual chunks are reassembled into the complete response before logging

### Session replay

Full session replay is available in the [Sessions page](https://app.formal.ai/sessions). The session replay shows every request sent and response received, including all tool call exchanges.

## Policies

You can enforce policies on LLM sessions at the `session`, `request`, and `response` stages. Use `session` rules to block a connection before it reaches the upstream provider, `request` rules to inspect or rewrite request headers and bodies, and `response` rules to inspect tool calls, the model, the provider, and token usage.

Request-stage rewrite policies can:

* Add, replace, or remove **headers** before Formal forwards the request
* Replace the **body** (for example with `regex.replace` on `input.http.body`) to redact sensitive prompt text such as SSNs without blocking the session

See [LLM policy inputs](https://docs.formal.ai/docs/guides/policies/evaluation.md#llm-resources) for the full list of available fields and examples, including [redacting SSNs from request messages](https://docs.formal.ai/docs/guides/policies/evaluation.md#example-redact-ssns-from-request-messages). To allow only your Anthropic organization, see [Restrict Anthropic Sign-Ins to One Org](https://docs.formal.ai/docs/guides/example-use-cases/restrict-anthropic-sign-ins.md).


## Related topics

- [LLM Gateway](https://docs.formal.ai/docs/guides/solutions/llm-gateway.md)
- [Control LLM Spend](https://docs.formal.ai/docs/guides/ai-governance/spend.md)
- [List LLM token prices](https://docs.formal.ai/api-reference/corev1llmpricingservice/list-llm-token-prices.md)
- [Delete LLM token price](https://docs.formal.ai/api-reference/corev1llmpricingservice/delete-llm-token-price.md)
- [Set LLM token price](https://docs.formal.ai/api-reference/corev1llmpricingservice/set-llm-token-price.md)


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