Skip to content

JSON Configuration

Instead of using the dashboard editor UI to define the route graph, you can do it using the REST API. Routes are internally represented using a simple JSON structure:

{
  "id": "<route id>",
  "name": "<route name>",
  "elements": [<array of elements>]
}

Request metadata

Conditional and rate/budget limit elements read data from the request's metadata only. They do not read the request body, headers, model, or task. AI Gateway does not detect a task or add it to metadata.

Reference metadata keys as metadata.<key>. The keys available are:

  • The custom metadata that you send in the cf-aig-metadata header. For example, if you send cf-aig-metadata: {"plan": "free", "task": "summarize"}, you can use metadata.plan and metadata.task.
  • metadata.cf.user_id, which AI Gateway adds when a user authenticates with Cloudflare Access. Service-token requests and requests without a user subject do not include this key. Refer to Reserved metadata.

To route on a value that AI Gateway does not supply, such as the task type, add it to cf-aig-metadata in your application.

Supported elements

Dynamic routing supports several types of elements that you can combine to create sophisticated routing flows. Each element has specific inputs, outputs, and configuration options.

Start Element

Marks the beginning of a route. Every route must start with a Start element.

  • Inputs: None
  • Outputs:
    • next: Forwards the unchanged request to the next element
{
	"id": "<id>",
	"type": "start",
	"outputs": {
		"next": { "elementId": "<id>" }
	}
}

Conditional Element (If/Else)

Evaluates a condition against the request metadata and routes the request accordingly.

  • Inputs: Request
  • Outputs:
    • true: Forwards request to provided element if condition evaluates to true
    • false: Forwards request to provided element if condition evaluates to false

conditions supports MongoDB-like operators such as $eq, $ne, $in, $and, and $or, plus $contains to match one entry in a list. If the request does not include a metadata key, the condition evaluates the key as missing. For example, $eq evaluates to false and $ne evaluates to true.

{
	"id": "<id>",
	"type": "conditional",
	"properties": {
		"conditions": {
			"metadata.plan": { "$eq": "free" }
		}
	},
	"outputs": {
		"true": { "elementId": "<id>" },
		"false": { "elementId": "<id>" }
	}
}

Match one entry in a list

Metadata values cannot be arrays. To send a list, such as the groups a user belongs to, send it as one comma-separated string and use $contains to check for one entry. This uses one custom metadata key for the whole list.

For example, if you send cf-aig-metadata: {"groups": "engineering,finance"}, the following condition evaluates to true:

{
	"conditions": {
		"metadata.groups": { "$contains": "finance" }
	}
}

$contains splits the value on commas, trims spaces around each entry and around the operand, and matches an entry that equals the operand exactly. It is case-sensitive and matches whole entries only, so finance does not match finance-readonly. It evaluates to false when the key is missing or its value is not a string. You can combine it with $and, $or, and $not.

Percentage Split

Routes requests probabilistically across multiple outputs, useful for A/B testing and gradual rollouts.

  • Inputs: Request
  • Outputs: Up to 5 named percentage outputs
    • Each output key (for example, "10%") is the probability for that branch, and the keys must sum to 100%
{
	"id": "<id>",
	"type": "percentage",
	"outputs": {
		"10%": { "elementId": "<id>" },
		"40%": { "elementId": "<id>" },
		"50%": { "elementId": "<id>" }
	}
}

Rate/Budget Limit

Apply limits based on request metadata. Supports both count-based and cost-based limits.

  • Inputs: Request
  • Outputs:
    • success: Forwards request to provided element if request is not rate limited
    • fallback: Optional output for rate-limited requests (route terminates if not provided)

Properties:

  • limitType: "count" or "cost"
  • key: Metadata key to use for rate limiting. For example, metadata.user_id for a custom key that your application sends, or metadata.cf.user_id for the Access user ID. Requests that do not include this key share one limit.
  • limit: Maximum allowed requests/cost
  • window: Time window in seconds
{
	"id": "<id>",
	"type": "rate",
	"properties": {
		"limitType": "count",
		"key": "metadata.user_id",
		"limit": 100,
		"window": 3600
	},
	"outputs": {
		"success": { "elementId": "node_model_workers_ai" },
		"fallback": { "elementId": "node_model_openai_mini" }
	}
}

Model

Executes inference using a specified model and provider with configurable timeout and retry settings.

  • Inputs: Request
  • Outputs:
    • success: Forwards request to provided element if model successfully starts streaming a response
    • fallback: Optional output if model fails after all retries or times out

Properties:

  • provider: AI provider (e.g. "openai", "anthropic")
  • model: Specific model name
  • timeout: Request timeout in milliseconds
  • retries: Number of retry attempts
{
	"id": "<id>",
	"type": "model",
	"properties": {
		"provider": "openai",
		"model": "gpt-4o-mini",
		"timeout": 60000,
		"retries": 4
	},
	"outputs": {
		"success": { "elementId": "<id>" },
		"fallback": { "elementId": "<id>" }
	}
}

End element

Marks the end of a route. Returns the last successful model response, or an error if no model response was generated.

  • Inputs: Request
  • Outputs: None (provide an empty outputs object)
{
	"id": "<id>",
	"type": "end",
	"outputs": {}
}

Was this helpful?