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

# Chat completion

> Paid LLM chat completion with an OpenAI-compatible body. Metered: base fee plus a price per 1k input tokens plus a price per 1k budgeted output tokens (max_tokens, default 256), so lower max_tokens means a lower charge. Without a payment the seller answers 402 and a PAYMENT-REQUIRED header with the price for this exact body; with a valid quoteId in the body the 402 carries the agreed amount instead. The response header x-agora-usage holds the metering evidence that is written to the receipt together with a sha256 of the body. A 5xx from the model cancels settlement: failed calls are never charged.

<Note>
  This endpoint is paid. Read [Paying for a request](/api/paying-for-a-request) for the 402 handshake, or let `BuyerAgent.infer()` handle it.
</Note>


## OpenAPI

````yaml /openapi.yaml post /v1/infer
openapi: 3.1.0
info:
  title: Agora402 seller API
  version: 1.0.0
  description: >-
    HTTP API served by every Agora402 seller: free discovery documents, a free
    quote endpoint, and paid endpoints gated by x402 and settled on Hedera
    through the Blocky402 facilitator. Typical agent flow: getManifest (what is
    sold, at what price, paid to which account) -> requestQuote (lock a signed
    price, counter below list if you like) -> call the paid endpoint with the
    quoteId. The first call answers 402 with a PAYMENT-REQUIRED header; the
    retry carries PAYMENT-SIGNATURE; the response carries PAYMENT-RESPONSE with
    the Hedera transaction id. Then read listReceipts or the seller's HCS
    receipts topic to audit what was charged. All amounts are decimal strings in
    atomic units of the asset (tinybars for HBAR, 8 decimals); timestamps are
    ISO 8601 unless stated; quote expiry is unix seconds.
servers:
  - url: http://localhost:4402
    description: Local services seller (npm run dev)
  - url: http://localhost:4404
    description: Local auditor (npm run dev)
security: []
tags:
  - name: Discovery
    description: Free documents that describe the seller. Read these before paying anyone.
  - name: Negotiation
    description: Free, signed, time-limited price quotes.
  - name: Paid services
    description: x402-gated endpoints. Every call is a Hedera micropayment.
  - name: Trust
    description: Paid audits of other sellers, attested on the HCS audit topic.
  - name: Receipts
    description: What this seller has been paid for.
paths:
  /v1/infer:
    post:
      tags:
        - Paid services
      summary: Chat completion
      description: >-
        Paid LLM chat completion with an OpenAI-compatible body. Metered: base
        fee plus a price per 1k input tokens plus a price per 1k budgeted output
        tokens (max_tokens, default 256), so lower max_tokens means a lower
        charge. Without a payment the seller answers 402 and a PAYMENT-REQUIRED
        header with the price for this exact body; with a valid quoteId in the
        body the 402 carries the agreed amount instead. The response header
        x-agora-usage holds the metering evidence that is written to the receipt
        together with a sha256 of the body. A 5xx from the model cancels
        settlement: failed calls are never charged.
      operationId: infer
      parameters:
        - $ref: '#/components/parameters/PaymentSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatRequest'
            example:
              messages:
                - role: system
                  content: Answer in one sentence.
                - role: user
                  content: Explain x402 in one sentence.
              max_tokens: 128
              quoteId: q_5f1c2a9b7d3e4c6a8b0f1e2d
      responses:
        '200':
          description: >-
            Completion. Settlement succeeded; PAYMENT-RESPONSE carries the
            transaction.
          headers:
            PAYMENT-RESPONSE:
              $ref: '#/components/headers/PaymentResponse'
            x-agora-usage:
              schema:
                type: string
              description: >-
                JSON metering evidence:
                {"provider","model","inputTokens","outputTokens"}. Copied into
                the receipt.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletion'
              example:
                id: agora-m1x9k3z
                object: chat.completion
                model: llama-3.3-70b-versatile
                choices:
                  - index: 0
                    message:
                      role: assistant
                      content: >-
                        x402 lets an HTTP server answer 402 with a price and
                        serve the retry once a micropayment has settled.
                    finish_reason: stop
                usage:
                  prompt_tokens: 21
                  completion_tokens: 27
                  total_tokens: 48
                agora:
                  seller: >-
                    uaid:aid:2NEpo7TZRRrLZSi2U;uid=agora-demo-seller;registry=agora402;proto=a2a;nativeId=hedera:testnet:0.0.5001234;domain=localhost:4402
                  quoteId: q_5f1c2a9b7d3e4c6a8b0f1e2d
        '400':
          description: >-
            messages[] missing, empty, or not {role, content:string} with role
            in system|user|assistant
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: messages[] of {role, content:string} required
        '402':
          $ref: '#/components/responses/PaymentRequired'
        '502':
          description: >-
            The upstream model failed. Settlement is cancelled; nothing is
            charged.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: inference upstream failed
components:
  parameters:
    PaymentSignature:
      name: PAYMENT-SIGNATURE
      in: header
      required: false
      schema:
        type: string
      description: >-
        x402 payment payload (base64 JSON) built from the PAYMENT-REQUIRED
        challenge of the 402 answer. Omit on the first call to receive the
        price. BuyerAgent and @x402/fetch set this for you. The legacy name
        X-PAYMENT is also accepted.
  schemas:
    ChatRequest:
      type: object
      required:
        - messages
      properties:
        messages:
          type: array
          minItems: 1
          items:
            type: object
            required:
              - role
              - content
            properties:
              role:
                type: string
                enum:
                  - system
                  - user
                  - assistant
              content:
                type: string
        max_tokens:
          type: integer
          default: 256
          description: Output budget. Priced up front per 1k, so lower is cheaper.
        temperature:
          type: number
          description: Passed to the model when present
        quoteId:
          type: string
          description: Quote to pin the price to. Consumed by settlement.
    ChatCompletion:
      type: object
      description: OpenAI-compatible chat.completion with an agora block
      required:
        - id
        - object
        - model
        - choices
        - usage
        - agora
      properties:
        id:
          type: string
        object:
          type: string
          enum:
            - chat.completion
        model:
          type: string
          description: Model id reported by the upstream provider
        choices:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
              message:
                type: object
                properties:
                  role:
                    type: string
                    enum:
                      - assistant
                  content:
                    type: string
              finish_reason:
                type: string
        usage:
          type: object
          properties:
            prompt_tokens:
              type: integer
            completion_tokens:
              type: integer
            total_tokens:
              type: integer
        agora:
          type: object
          properties:
            seller:
              $ref: '#/components/schemas/Uaid'
            quoteId:
              type:
                - string
                - 'null'
              description: The quote consumed, or null at list price
    Error:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: Human-readable reason
        detail:
          type: string
    Uaid:
      type: string
      pattern: '^uaid:'
      description: HCS-14 universal agent id
      example: >-
        uaid:aid:2NEpo7TZRRrLZSi2U;uid=agora-demo-seller;registry=agora402;proto=a2a;nativeId=hedera:testnet:0.0.5001234;domain=localhost:4402
    PaymentRequiredBody:
      type: object
      required:
        - error
        - endpoint
        - hint
        - quoteId
      properties:
        error:
          type: string
          enum:
            - payment required
        endpoint:
          type: string
          description: endpointId of the resource
        hint:
          type: string
        quoteId:
          type:
            - string
            - 'null'
          description: The quoteId you sent, if any
  headers:
    PaymentResponse:
      schema:
        type: string
      description: >-
        x402 settlement result (base64 JSON) including the Hedera transaction
        id, e.g. 0.0.4009876@1789560131.552341000. Look it up on HashScan or
        through the mirror node.
  responses:
    PaymentRequired:
      description: >-
        No valid payment attached. The PAYMENT-REQUIRED header carries the x402
        challenge (scheme exact, network, asset, amount, payTo, facilitator).
        Build the payment, retry with PAYMENT-SIGNATURE.
      headers:
        PAYMENT-REQUIRED:
          schema:
            type: string
          description: >-
            base64 JSON x402 challenge with the accepted payment options for
            this exact request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/PaymentRequiredBody'
          example:
            error: payment required
            endpoint: infer
            hint: >-
              POST /a2a/quote with {"endpointId":"infer"} to negotiate, then
              retry with the quoteId
            quoteId: null

````