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

# Write working memory

> An agent's working state, written by its code and never by a model: `cas` replaces the whole state at a version, `merge_by_key` replaces the named fields. Over the cap it answers `stored: false`, never an error. The `agent_state` feature.



## OpenAPI

````yaml openapi/en/cell.json PUT /v1/agent-state
openapi: 3.1.0
info:
  title: Niadra data API
  version: '1'
  description: >-
    Writing, context, history, objects, identity, privacy and governance of one
    space. Every space has a stable address, with the space and the region in
    its name.
servers:
  - url: https://{space}.{region}.api.niadra.com
    variables:
      space:
        default: acme-prod
        description: The space, which comes in the source key.
      region:
        default: us-east-2
        description: The region of the space, which also comes in the key.
security: []
paths:
  /v1/agent-state:
    put:
      tags:
        - state
      summary: Write working memory
      description: >-
        A write by the agent's code: over the cap it answers `stored: false`,
        never an error.


        **Authentication.** Source key: `Authorization: Bearer nia_sk_...`.
        Required scope: `agent_state`. It answers only in a space with the
        `agent_state` feature on (the `features` document); in a space without
        it, 404.
      operationId: write_agent_state_v1_agent_state_put
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgentStateWrite'
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgentStateWriteResult'
          description: Successful Response
        '412':
          description: '`agent_state_conflict`: the version moved since `if_version`.'
        '422':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Problem'
          description: The request does not match the contract.
      security:
        - sourceKey: []
      x-codeSamples:
        - lang: python
          label: Python
          source: >-
            # Written by the agent's code, never by a model; merge by key keeps
            the other fields

            written = conversation.agent_state.put({"offer": {"plan":
            "familia-plus", "status": "shown"}})

            if not written.stored:
                print(written.reason)  # over_cap or not_declared_field, never an exception
        - lang: typescript
          label: TypeScript
          source: >-
            // Written by the agent's code, never by a model; merge by key keeps
            the other fields

            const written = await convo.agentState.put({ offer: { plan:
            "familia-plus", status: "shown" } });

            if (!written.stored) console.log(written.reason); // over_cap or
            not_declared_field, never a rejection
components:
  schemas:
    AgentStateWrite:
      additionalProperties: false
      description: >-
        A write of an agent's working state, by its code and never by a model:
        `cas` replaces the whole state

        at `if_version` (0 when it must not exist yet); `merge_by_key` replaces
        each top-level field it names and

        removes a field written as exactly `{"$delete": true}`.
      properties:
        agent:
          maxLength: 256
          minLength: 1
          title: Agent
          type: string
        body:
          additionalProperties: true
          title: Body
          type: object
        if_version:
          anyOf:
            - minimum: 0
              type: integer
            - type: 'null'
          title: If Version
        mode:
          enum:
            - cas
            - merge_by_key
          title: Mode
          type: string
        scope:
          $ref: '#/components/schemas/AgentStateScope'
        subject:
          anyOf:
            - $ref: '#/components/schemas/Handle'
            - type: 'null'
          description: >-
            The customer the state is about, when its scope does not name them:
            erasing the customer and their data package find the state by it.
      required:
        - scope
        - agent
        - mode
        - body
      title: AgentStateWrite
      type: object
    AgentStateWriteResult:
      additionalProperties: false
      properties:
        reason:
          anyOf:
            - enum:
                - over_cap
                - not_declared_field
              type: string
            - type: 'null'
          title: Reason
        stored:
          title: Stored
          type: boolean
        version:
          title: Version
          type: integer
      required:
        - stored
        - version
      title: AgentStateWriteResult
      type: object
    Problem:
      additionalProperties: false
      description: RFC 9457 problem details; `code` comes from the versioned error catalog.
      properties:
        code:
          title: Code
          type: string
        detail:
          anyOf:
            - type: string
            - type: 'null'
          default: null
          title: Detail
        request_id:
          anyOf:
            - type: string
            - type: 'null'
          default: null
          title: Request Id
        status:
          title: Status
          type: integer
        title:
          title: Title
          type: string
        type:
          default: about:blank
          title: Type
          type: string
      required:
        - title
        - status
        - code
      title: Problem
      type: object
    AgentStateScope:
      additionalProperties: false
      description: >-
        What one working state belongs to. The id travels in the body, never in
        a URL: a conversation id may

        look like a phone number.
      properties:
        id:
          maxLength: 512
          minLength: 1
          title: Id
          type: string
        kind:
          enum:
            - conversation
            - task
            - object
            - subject
          title: Kind
          type: string
      required:
        - kind
        - id
      title: AgentStateScope
      type: object
    Handle:
      additionalProperties: false
      description: >-
        An identifier of a subject in some channel or system: a phone, an
        e-mail, a CRM id.
      properties:
        scope:
          anyOf:
            - maxLength: 256
              minLength: 1
              type: string
            - type: 'null'
          description: >-
            Namespace for scoped identifiers: the WhatsApp Business account for
            `wa_bsuid`, the system for `system_id`, the country for
            `gov_id_hmac`.
          title: Scope
        subject_kind:
          anyOf:
            - $ref: '#/components/schemas/SubjectKind'
            - type: 'null'
          description: Defaults to `person`, except for organization-only handle types.
        type:
          $ref: '#/components/schemas/HandleType'
        value:
          maxLength: 320
          minLength: 1
          title: Value
          type: string
          description: >-
            The identifier. Normalized on the server: E.164 for phones,
            lowercase for e-mail.
      required:
        - type
        - value
      title: Handle
      type: object
    SubjectKind:
      enum:
        - person
        - account
        - partner
      title: SubjectKind
      type: string
      description: >-
        A person, a customer organization (`account`) or an organization that
        takes part without being a customer (`partner`).
    HandleType:
      enum:
        - phone_e164
        - wa_id
        - wa_jid
        - wa_lid
        - wa_bsuid
        - email
        - gov_id_hmac
        - app_user_id
        - system_id
        - org_registry_hmac
        - email_domain
        - anon_id
      title: HandleType
      type: string
      description: >-
        The kind of identifier. The value is classified by its format, never by
        the field it came from.
  securitySchemes:
    sourceKey:
      type: http
      scheme: bearer
      description: nia_sk_...

````