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

# Create custom metric definition

> Create a new metric definition. The `calculationType` field selects the variant: LLM_JUDGE (LLM-evaluated), FORMULA (computed from a math expression over other metrics), or PATTERN (detects a trigger→outcome pattern within a window). To create a threshold on top of an existing metric, use `POST /metric/definitions/{idOrSlug}/thresholds` instead.



## OpenAPI

````yaml /api-reference/openapi.documented.json post /v1/metric/definitions
openapi: 3.1.0
info:
  title: Roark Analytics API
  description: >-
    The Roark Analytics API gives you access to the same API that powers the
    award winning Roark Analytics platform.
  version: 1.0.0
servers:
  - description: Production
    url: https://api.roark.ai
security:
  - Bearer: []
tags:
  - name: Agent
  - name: Agent Endpoint
  - name: Call
  - name: Chat
  - name: Metric
  - name: Metric Policy
  - name: Metric Collection Job
  - name: Simulation Persona
  - name: Simulation Scenario
  - name: Simulation Run Plan
  - name: Simulation Run Plan Job
  - name: Simulation Job
  - name: HTTP Request Definition
  - name: Webhook
  - name: Issue
  - name: Knowledge Base
  - name: Call Analysis
  - name: Health
paths:
  /v1/metric/definitions:
    post:
      tags:
        - Metric
      summary: Create custom metric definition
      description: >-
        Create a new metric definition. The `calculationType` field selects the
        variant: LLM_JUDGE (LLM-evaluated), FORMULA (computed from a math
        expression over other metrics), or PATTERN (detects a trigger→outcome
        pattern within a window). To create a threshold on top of an existing
        metric, use `POST /metric/definitions/{idOrSlug}/thresholds` instead.
      operationId: postV1MetricDefinitions
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateMetricDefinitionInput'
      responses:
        '201':
          description: The created metric definition
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    $ref: '#/components/schemas/CreateMetricDefinitionResponse'
                required:
                  - data
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
                description: Validation error
              example:
                type: validation
                code: invalid_parameter
                message: The request was invalid
                param: email
          description: Bad Request
        '401':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
                description: Authentication error
              example:
                type: authentication
                code: unauthorized
                message: Authentication required
          description: Unauthorized
        '403':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
                description: Permission error
              example:
                type: forbidden
                code: permission_denied
                message: You do not have permission to access this resource
          description: Forbidden
        '429':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
                description: Rate limit error
              example:
                type: rate_limit
                code: too_many_requests
                message: Rate limit exceeded
          description: Too Many Requests
        '500':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
                description: Server error
              example:
                type: internal
                code: internal_error
                message: Internal server error
          description: Internal Server Error
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import Roark from '@roarkanalytics/sdk';

            const client = new Roark({
              bearerToken: process.env['ROARK_API_BEARER_TOKEN'], // This is the default and can be omitted
            });

            const response = await client.metric.createDefinition({
              analysisPackageId: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e',
              calculationType: 'LLM_JUDGE',
              name: 'Customer Satisfaction',
              outputType: 'BOOLEAN',
            });

            console.log(response.data);
        - lang: Python
          source: |-
            import os
            from roark_analytics import Roark

            client = Roark(
                bearer_token=os.environ.get("ROARK_API_BEARER_TOKEN"),  # This is the default and can be omitted
            )
            response = client.metric.create_definition(
                analysis_package_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
                calculation_type="LLM_JUDGE",
                name="Customer Satisfaction",
                output_type="BOOLEAN",
            )
            print(response.data)
components:
  schemas:
    CreateMetricDefinitionInput:
      oneOf:
        - $ref: '#/components/schemas/PromptMetricInput'
        - $ref: '#/components/schemas/FormulaMetricInput'
        - $ref: '#/components/schemas/PatternMetricInput'
      discriminator:
        propertyName: calculationType
        mapping:
          LLM_JUDGE:
            $ref: '#/components/schemas/PromptMetricInput'
          FORMULA:
            $ref: '#/components/schemas/FormulaMetricInput'
          PATTERN:
            $ref: '#/components/schemas/PatternMetricInput'
      description: >-
        Input for creating a new metric definition. `calculationType` selects
        the variant and defaults to LLM_JUDGE when omitted (legacy prompt-metric
        payloads remain valid). To create a threshold, use the threshold
        sub-resource.
    CreateMetricDefinitionResponse:
      oneOf:
        - $ref: '#/components/schemas/LlmJudgeMetricResponse'
        - $ref: '#/components/schemas/FormulaMetricResponse'
        - $ref: '#/components/schemas/PatternMetricResponse'
      discriminator:
        propertyName: calculationType
        mapping:
          LLM_JUDGE:
            $ref: '#/components/schemas/LlmJudgeMetricResponse'
          FORMULA:
            $ref: '#/components/schemas/FormulaMetricResponse'
          PATTERN:
            $ref: '#/components/schemas/PatternMetricResponse'
      description: >-
        The created metric definition. The variant is selected by
        `calculationType`.
    ErrorResponse:
      type: object
      properties:
        type:
          type: string
          enum:
            - validation
            - authentication
            - forbidden
            - not_found
            - conflict
            - rate_limit
            - internal
          description: The error type category
          examples:
            - validation
            - authentication
        code:
          type: string
          description: Machine-readable error code identifier
          examples:
            - invalid_parameter
            - missing_required_field
            - unauthorized
        message:
          type: string
          description: Human-readable error message
          examples:
            - The request was invalid
            - Authentication required
        param:
          type: string
          description: The parameter that caused the error (if applicable)
          examples:
            - email
            - user_id
        details:
          description: Additional error context information
      required:
        - type
        - code
        - message
    PromptMetricInput:
      type: object
      properties:
        calculationType:
          type: string
          const: LLM_JUDGE
          description: LLM-evaluated metric.
        name:
          type: string
          minLength: 1
          maxLength: 100
          description: Name of the metric
          example: Customer Satisfaction
        slug:
          type: string
          minLength: 1
          maxLength: 50
          description: Stable slug for the metric. Auto-generated from name if omitted.
          example: customer_satisfaction
        metricId:
          type: string
          minLength: 1
          maxLength: 50
          description: >-
            Alias of `slug` accepted for backwards compatibility. Use `slug` for
            new integrations.
          example: customer_satisfaction
        outputType:
          type: string
          enum:
            - COUNT
            - NUMERIC
            - BOOLEAN
            - SCALE
            - TEXT
            - CLASSIFICATION
            - OFFSET
          description: Type of value this metric produces
          example: BOOLEAN
        scope:
          type: string
          enum:
            - GLOBAL
            - PER_PARTICIPANT
          default: GLOBAL
          description: 'Whether metric is global or per-participant (default: GLOBAL)'
        participantRole:
          type: string
          enum:
            - AGENT
            - CUSTOMER
            - SIMULATED_CUSTOMER
            - BACKGROUND_SPEAKER
          description: >-
            Participant role to evaluate. Required when scope is
            PER_PARTICIPANT.
        supportedContexts:
          type: array
          items:
            type: string
            enum:
              - CALL
              - SEGMENT
              - TURN
          minItems: 1
          default:
            - CALL
          description: 'Which levels this metric can produce values at (default: ["CALL"])'
        llmPrompt:
          type: string
          maxLength: 2000
          description: >-
            LLM prompt/criteria for evaluating this metric. Required for
            BOOLEAN, NUMERIC, TEXT, and SCALE types.
          example: >-
            Evaluate whether the customer expressed satisfaction with the
            service provided.
        analysisPackageId:
          type: string
          format: uuid
          description: ID of the analysis package to add this metric to
        booleanTrueLabel:
          type: string
          description: Label for the true case (only for BOOLEAN type)
        booleanFalseLabel:
          type: string
          description: Label for the false case (only for BOOLEAN type)
        scaleMin:
          type: integer
          minimum: 0
          maximum: 100
          description: Minimum value for scale. Required for SCALE type.
        scaleMax:
          type: integer
          minimum: 0
          maximum: 100
          description: Maximum value for scale. Required for SCALE type.
        scaleLabels:
          type: array
          items:
            type: object
            properties:
              rangeMin:
                type: number
                description: Minimum value for this label range
              rangeMax:
                type: number
                description: Maximum value for this label range
              label:
                type: string
                description: Label for this range
              description:
                type: string
                description: Description of what this range means
              displayOrder:
                type: integer
                description: Display order of this label
              colorHex:
                type: string
                description: Hex color code for this label (e.g. "#FF0000")
            required:
              - rangeMin
              - rangeMax
              - label
              - displayOrder
          description: Labels for scale ranges (only for SCALE type)
        classificationOptions:
          type: array
          items:
            type: object
            properties:
              label:
                type: string
              description:
                type: string
              displayOrder:
                type: integer
            required:
              - label
              - description
              - displayOrder
            description: Option for classification metrics.
          minItems: 1
          description: Options for classification. Required for CLASSIFICATION type.
        maxClassifications:
          type: integer
          minimum: 1
          description: >-
            Maximum number of classifications that can be selected (only for
            CLASSIFICATION type)
      required:
        - calculationType
        - name
        - outputType
        - analysisPackageId
      title: LLM judge metric
    FormulaMetricInput:
      type: object
      properties:
        calculationType:
          type: string
          const: FORMULA
          description: >-
            Metric computed by evaluating a mathematical expression over other
            metrics.
        name:
          type: string
          minLength: 1
          maxLength: 100
          description: Name of the metric
          example: Customer Satisfaction
        slug:
          type: string
          minLength: 1
          maxLength: 50
          description: Stable slug for the metric. Auto-generated from name if omitted.
          example: customer_satisfaction
        metricId:
          type: string
          minLength: 1
          maxLength: 50
          description: >-
            Alias of `slug` accepted for backwards compatibility. Use `slug` for
            new integrations.
          example: customer_satisfaction
        outputType:
          type: string
          enum:
            - NUMERIC
            - BOOLEAN
          description: >-
            Output type of the formula. NUMERIC for arithmetic expressions,
            BOOLEAN for comparison expressions.
          example: NUMERIC
        formula:
          type: string
          minLength: 1
          maxLength: 500
          description: >-
            Formula expression using `{{id:<uuid>}}` references to source
            metrics. Operators depend on output type: +, -, *, / for NUMERIC;
            ==, !=, >=, <=, >, < for BOOLEAN.
          example: >-
            {{id:00000000-0000-0000-0000-000000000001}} /
            {{id:00000000-0000-0000-0000-000000000002}}
        sources:
          type: array
          items:
            type: object
            properties:
              sourceMetricDefinitionId:
                type: string
                format: uuid
                description: ID of a metric referenced in the formula
              sourceVariantId:
                type: string
                format: uuid
                description: Variant of the source metric to use
            required:
              - sourceMetricDefinitionId
          minItems: 2
          description: Source metrics referenced by the formula. Minimum 2.
        analysisPackageId:
          type: string
          format: uuid
          description: ID of the analysis package to add this metric to
      required:
        - calculationType
        - name
        - outputType
        - formula
        - sources
        - analysisPackageId
      title: Formula metric
    PatternMetricInput:
      type: object
      properties:
        calculationType:
          type: string
          const: PATTERN
          description: >-
            Metric detecting temporal patterns: a trigger condition followed by
            an outcome within a window.
        name:
          type: string
          minLength: 1
          maxLength: 100
          description: Name of the metric
          example: Customer Satisfaction
        slug:
          type: string
          minLength: 1
          maxLength: 50
          description: Stable slug for the metric. Auto-generated from name if omitted.
          example: customer_satisfaction
        metricId:
          type: string
          minLength: 1
          maxLength: 50
          description: >-
            Alias of `slug` accepted for backwards compatibility. Use `slug` for
            new integrations.
          example: customer_satisfaction
        operation:
          type: string
          enum:
            - PATTERN_EXISTS
            - PATTERN_COUNT
            - OUTCOME_AGGREGATE
          description: >-
            Pattern operation. PATTERN_EXISTS produces a BOOLEAN; PATTERN_COUNT
            produces a NUMERIC count; OUTCOME_AGGREGATE aggregates a numeric
            outcome.
          example: PATTERN_EXISTS
        windowMode:
          type: string
          enum:
            - seconds
            - segments
          default: seconds
          description: 'Unit for trigger/outcome window values (default: seconds)'
        trigger:
          type: object
          properties:
            sourceMetricDefinitionId:
              type: string
              format: uuid
            operator:
              type: string
              enum:
                - GREATER_THAN
                - GREATER_THAN_OR_EQUALS
                - LESS_THAN
                - LESS_THAN_OR_EQUALS
                - EQUALS
                - NOT_EQUALS
            thresholdValue:
              type: string
              minLength: 1
            sourceParticipantRole:
              type: string
              enum:
                - AGENT
                - CUSTOMER
                - SIMULATED_CUSTOMER
                - BACKGROUND_SPEAKER
            sourceVariantId:
              type: string
              format: uuid
          required:
            - sourceMetricDefinitionId
            - operator
            - thresholdValue
          description: >-
            Single trigger condition. Use either trigger or triggers +
            triggerCombinator.
        triggers:
          type: array
          items:
            type: object
            properties:
              sourceMetricDefinitionId:
                type: string
                format: uuid
              operator:
                type: string
                enum:
                  - GREATER_THAN
                  - GREATER_THAN_OR_EQUALS
                  - LESS_THAN
                  - LESS_THAN_OR_EQUALS
                  - EQUALS
                  - NOT_EQUALS
              thresholdValue:
                type: string
                minLength: 1
              sourceParticipantRole:
                type: string
                enum:
                  - AGENT
                  - CUSTOMER
                  - SIMULATED_CUSTOMER
                  - BACKGROUND_SPEAKER
              sourceVariantId:
                type: string
                format: uuid
            required:
              - sourceMetricDefinitionId
              - operator
              - thresholdValue
          minItems: 1
          description: Multiple trigger conditions. Use with triggerCombinator.
        triggerCombinator:
          type: string
          enum:
            - AND
            - OR
          description: >-
            How to combine multiple triggers. Required when triggers has more
            than 1 entry.
        outcome:
          type: object
          properties:
            sourceMetricDefinitionId:
              type: string
              format: uuid
            operator:
              type: string
              enum:
                - GREATER_THAN
                - GREATER_THAN_OR_EQUALS
                - LESS_THAN
                - LESS_THAN_OR_EQUALS
                - EQUALS
                - NOT_EQUALS
            thresholdValue:
              type: string
              minLength: 1
            sourceParticipantRole:
              type: string
              enum:
                - AGENT
                - CUSTOMER
                - SIMULATED_CUSTOMER
                - BACKGROUND_SPEAKER
            sourceVariantId:
              type: string
              format: uuid
            windowAfter:
              type: integer
              minimum: 0
              description: >-
                How far after the trigger to look for the outcome (in seconds or
                segments, see windowMode)
            windowBefore:
              type: integer
              minimum: 0
              description: 'How far before the trigger to look for the outcome (default: 0)'
          required:
            - sourceMetricDefinitionId
            - operator
            - thresholdValue
            - windowAfter
          description: >-
            Outcome condition evaluated within the window relative to the
            trigger.
        analysisPackageId:
          type: string
          format: uuid
          description: ID of the analysis package to add this metric to
      required:
        - calculationType
        - name
        - operation
        - outcome
        - analysisPackageId
      title: Pattern metric
    LlmJudgeMetricResponse:
      type: object
      properties:
        id:
          type: string
          format: uuid
          description: Unique identifier for the metric definition
        slug:
          type: string
          description: Stable metric slug (e.g. "call_reason", "customer_satisfaction")
        metricId:
          type: string
          description: >-
            Alias of `slug` retained for backwards compatibility. Same value as
            `slug`.
        variantId:
          type: string
          format: uuid
          description: >-
            The resolved variant this response reflects (org-scoped Default if
            the org has customized it, otherwise the system Default). Pass this
            as sourceVariantId when building a derived metric off this one to
            pin the exact config.
        versionId:
          type: string
          format: uuid
          description: >-
            The variant's current version. Immutable snapshot of the config —
            editing the metric produces a new versionId. Use it to detect config
            changes.
        name:
          type: string
          description: Name of the metric
        description:
          type: string
          description: Description of what the metric measures
        type:
          type: string
          enum:
            - COUNT
            - NUMERIC
            - BOOLEAN
            - SCALE
            - TEXT
            - CLASSIFICATION
            - OFFSET
          description: Type of value this metric produces
        scope:
          type: string
          enum:
            - GLOBAL
            - PER_PARTICIPANT
          description: Whether metric is global or per-participant
        supportedContexts:
          type: array
          items:
            type: string
            enum:
              - CALL
              - SEGMENT
              - TURN
          description: Which levels this metric can produce values at
        unit:
          type: object
          properties:
            name:
              type: string
              description: Name of the unit
            symbol:
              type:
                - string
                - 'null'
              description: Symbol for the unit
          required:
            - name
            - symbol
          description: Unit information if applicable
        calculationType:
          type: string
          const: LLM_JUDGE
          description: Metric evaluated by an LLM against a prompt.
      required:
        - id
        - slug
        - metricId
        - variantId
        - versionId
        - name
        - description
        - type
        - scope
        - supportedContexts
        - calculationType
      title: LLM judge metric
    FormulaMetricResponse:
      type: object
      properties:
        id:
          type: string
          format: uuid
          description: Unique identifier for the metric definition
        slug:
          type: string
          description: Stable metric slug (e.g. "call_reason", "customer_satisfaction")
        metricId:
          type: string
          description: >-
            Alias of `slug` retained for backwards compatibility. Same value as
            `slug`.
        variantId:
          type: string
          format: uuid
          description: >-
            The resolved variant this response reflects (org-scoped Default if
            the org has customized it, otherwise the system Default). Pass this
            as sourceVariantId when building a derived metric off this one to
            pin the exact config.
        versionId:
          type: string
          format: uuid
          description: >-
            The variant's current version. Immutable snapshot of the config —
            editing the metric produces a new versionId. Use it to detect config
            changes.
        name:
          type: string
          description: Name of the metric
        description:
          type: string
          description: Description of what the metric measures
        type:
          type: string
          enum:
            - COUNT
            - NUMERIC
            - BOOLEAN
            - SCALE
            - TEXT
            - CLASSIFICATION
            - OFFSET
          description: Type of value this metric produces
        scope:
          type: string
          enum:
            - GLOBAL
            - PER_PARTICIPANT
          description: Whether metric is global or per-participant
        supportedContexts:
          type: array
          items:
            type: string
            enum:
              - CALL
              - SEGMENT
              - TURN
          description: Which levels this metric can produce values at
        unit:
          type: object
          properties:
            name:
              type: string
              description: Name of the unit
            symbol:
              type:
                - string
                - 'null'
              description: Symbol for the unit
          required:
            - name
            - symbol
          description: Unit information if applicable
        calculationType:
          type: string
          const: FORMULA
          description: Metric computed by evaluating an expression over other metrics.
        formula:
          $ref: '#/components/schemas/FormulaMetricDetails'
          description: Formula configuration.
      required:
        - id
        - slug
        - metricId
        - variantId
        - versionId
        - name
        - description
        - type
        - scope
        - supportedContexts
        - calculationType
        - formula
      title: Formula metric
    PatternMetricResponse:
      type: object
      properties:
        id:
          type: string
          format: uuid
          description: Unique identifier for the metric definition
        slug:
          type: string
          description: Stable metric slug (e.g. "call_reason", "customer_satisfaction")
        metricId:
          type: string
          description: >-
            Alias of `slug` retained for backwards compatibility. Same value as
            `slug`.
        variantId:
          type: string
          format: uuid
          description: >-
            The resolved variant this response reflects (org-scoped Default if
            the org has customized it, otherwise the system Default). Pass this
            as sourceVariantId when building a derived metric off this one to
            pin the exact config.
        versionId:
          type: string
          format: uuid
          description: >-
            The variant's current version. Immutable snapshot of the config —
            editing the metric produces a new versionId. Use it to detect config
            changes.
        name:
          type: string
          description: Name of the metric
        description:
          type: string
          description: Description of what the metric measures
        type:
          type: string
          enum:
            - COUNT
            - NUMERIC
            - BOOLEAN
            - SCALE
            - TEXT
            - CLASSIFICATION
            - OFFSET
          description: Type of value this metric produces
        scope:
          type: string
          enum:
            - GLOBAL
            - PER_PARTICIPANT
          description: Whether metric is global or per-participant
        supportedContexts:
          type: array
          items:
            type: string
            enum:
              - CALL
              - SEGMENT
              - TURN
          description: Which levels this metric can produce values at
        unit:
          type: object
          properties:
            name:
              type: string
              description: Name of the unit
            symbol:
              type:
                - string
                - 'null'
              description: Symbol for the unit
          required:
            - name
            - symbol
          description: Unit information if applicable
        calculationType:
          type: string
          const: PATTERN
          description: >-
            Metric detecting a trigger condition followed by an outcome within a
            window.
        pattern:
          $ref: '#/components/schemas/PatternMetricDetails'
          description: Pattern configuration.
      required:
        - id
        - slug
        - metricId
        - variantId
        - versionId
        - name
        - description
        - type
        - scope
        - supportedContexts
        - calculationType
        - pattern
      title: Pattern metric
    FormulaMetricDetails:
      type: object
      properties:
        expression:
          type: string
        sources:
          type: array
          items:
            type: object
            properties:
              sourceMetricDefinitionId:
                type: string
                format: uuid
              sourceVariantId:
                type:
                  - string
                  - 'null'
                format: uuid
            required:
              - sourceMetricDefinitionId
              - sourceVariantId
      required:
        - expression
        - sources
    PatternMetricDetails:
      type: object
      properties:
        operation:
          type: string
          enum:
            - PATTERN_EXISTS
            - PATTERN_COUNT
            - OUTCOME_AGGREGATE
        windowMode:
          type:
            - string
            - 'null'
        triggerCombinator:
          type:
            - string
            - 'null'
          enum:
            - AND
            - OR
        triggers:
          type: array
          items:
            type: object
            properties:
              sourceMetricDefinitionId:
                type: string
                format: uuid
              operator:
                type: string
                enum:
                  - GREATER_THAN
                  - GREATER_THAN_OR_EQUALS
                  - LESS_THAN
                  - LESS_THAN_OR_EQUALS
                  - EQUALS
                  - NOT_EQUALS
              thresholdValue:
                type: string
              sourceParticipantRole:
                type:
                  - string
                  - 'null'
                enum:
                  - AGENT
                  - CUSTOMER
                  - SIMULATED_CUSTOMER
                  - BACKGROUND_SPEAKER
              sourceVariantId:
                type:
                  - string
                  - 'null'
                format: uuid
            required:
              - sourceMetricDefinitionId
              - operator
              - thresholdValue
              - sourceParticipantRole
              - sourceVariantId
        outcome:
          type:
            - object
            - 'null'
          properties:
            sourceMetricDefinitionId:
              type: string
              format: uuid
            operator:
              type: string
              enum:
                - GREATER_THAN
                - GREATER_THAN_OR_EQUALS
                - LESS_THAN
                - LESS_THAN_OR_EQUALS
                - EQUALS
                - NOT_EQUALS
            thresholdValue:
              type: string
            sourceParticipantRole:
              type:
                - string
                - 'null'
              enum:
                - AGENT
                - CUSTOMER
                - SIMULATED_CUSTOMER
                - BACKGROUND_SPEAKER
            sourceVariantId:
              type:
                - string
                - 'null'
              format: uuid
            windowBefore:
              type:
                - integer
                - 'null'
            windowAfter:
              type:
                - integer
                - 'null'
          required:
            - sourceMetricDefinitionId
            - operator
            - thresholdValue
            - sourceParticipantRole
            - sourceVariantId
            - windowBefore
            - windowAfter
      required:
        - operation
        - windowMode
        - triggerCombinator
        - triggers
        - outcome
  securitySchemes:
    Bearer:
      type: http
      scheme: bearer
      bearerFormat: JWT

````