Configuring policies in code view

Discover the prerequisites for authoring policies in the code view.

IBM API Studio applies policies to control behavior, security, and traffic management of MCP servers and LLM provider APIs. The policies are defined individually in YAML format and are grouped in a policy sequence file to enforce multiple policies in a specific order.

Policy structure

Each policy is defined in a separate file, specifying the rules that it enforces. The kind attribute in the policy file identifies the policy type, such as Transport or Routing, determining its function within the API configuration.

A policy file includes the following fields:

  • kind. Specifies the asset type, such as URISchemes.
  • apiVersion. Defines the API schema version.
  • metadata. Specifies details like name, namespace, and version.
  • spec. Contains policy-specific configuration.

A sample for individual policy is as follows:

kind: Parse
apiVersion: api.ibm.com/v1
metadata:
  name: Parse-p2k4a
  namespace: DPNano_Product_LLM_Provider
  version: '1.0'
  tags: []
  domain: ai-platform
  labels:
    gatewayTypes:
      - nano
spec:
  documentType:
    json: {}
  input: request

This Parse policy converts raw input into parsed JSON or XML.

Policy sequence structure

A policy sequence defines the order in which policies are executed during request processing. It orchestrates multiple policies into a complete execution flow.
Note: The LLMProvider kind file values take precedence over individual policy kind file values. Therefore, you must modify only the LLMProvider kind file.

A policy file includes the following fields:

  • kind. Specifies the asset type. For policy sequences, the value is FreeFlowPolicySequence.
  • apiVersion. Defines the API schema version.
  • metadata. Specifies details like name, namespace, version, and labels.
  • spec. Defines the execution flow of policies, grouped into logical phases such as main and monitoring.
A sample for FreeFlowPolicySequence for an LLM provider is as follows:
kind: FreeFlowPolicySequence
apiVersion: api.ibm.com/v1
metadata:
  name: weather-api-seq
  namespace: DPNano_Product_MCPServer
  version: "1.0"
  tags: []
  labels:
    gatewayTypes:
      - nano
spec:
  main:
    - $ref: DPNano_Product_MCPServer:invoke:1.0
  monitoring:
    $ref: DPNano_Product_MCPServer:telemetry:1.0
A sample for FreeFlowPolicySequence for an MCP server is as follows:
kind: FreeFlowPolicySequence
apiVersion: api.ibm.com/v1
metadata:
  name: freeflowpolicysequence-watsonx-conn-sample-p2k4a
  namespace: DPNano_Product_LLM_Provider
  version: '1.0'
  tags: []
  domain: ai-platform
  labels:
    gatewayTypes:
      - nano
spec:
  main:
    - $ref: DPNano_Product_LLM_Provider:ExtractIdentity-p2k4a:1.0
    - $ref: DPNano_Product_LLM_Provider:Authenticate-p2k4a:1.0
    - $ref: DPNano_Product_LLM_Provider:Authorize-p2k4a:1.0
    - $ref: DPNano_Product_LLM_Provider:my-rate-limit:1.0
    - $ref: DPNano_Product_LLM_Provider:IBMCloudLogin-p2k4a:1.0
    - $ref: DPNano_Product_LLM_Provider:Parse-p2k4a:1.0
    - $ref: DPNano_Product_LLM_Provider:OperationSwitch-p2k4a:1.0
  monitoring:
    $ref: DPNano_Product_LLM_Provider:Telemetry-p2k4a:1.0