Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
agent / agent-contextBuilt by davevia claude-codeRuns on Liberasruns as agent-context-3

Context window

A message costs ceil(UTF-8 bytes of its content / 4) + 4 tokens; estimate(messages) is the sum. fit(messages, limit) keeps every system message, then walks the other messages from the newest to the oldest adding each whose cost still fits within limit (together with everything kept so far) and stops at the first that does not; the kept messages are returned in their original order. System messages are kept even when they alone exceed the limit.

Interface (WIT)31 lines›
agent-context.wit
package root:component;

world root {
  import liberas:agent/types@0.1.0;

  export liberas:agent/context@0.1.0;
}
package liberas:agent@0.1.0 {
  interface types {
    enum role {
      system,
      user,
      assistant,
      tool,
    }

    record message {
      role: role,
      content: string,
      tool-call-id: option<string>,
    }
  }
  interface context {
    use types.{message};

    fit: func(messages: list<message>, limit: u32) -> list<message>;

    estimate: func(messages: list<message>) -> u32;
  }
}
Test vectors12›
agent-context.cases.json
[
  {
    "fn": "estimate",
    "args": {
      "messages": []
    }
  },
  {
    "fn": "estimate",
    "args": {
      "messages": [
        {
          "role": "system",
          "content": "You are a careful assistant.",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "estimate",
    "args": {
      "messages": [
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "tool",
          "content": "ok",
          "tool-call-id": "c1"
        }
      ]
    }
  },
  {
    "fn": "estimate",
    "args": {
      "messages": [
        {
          "role": "user",
          "content": "한글은 세 바이트",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 20,
      "messages": [
        {
          "role": "system",
          "content": "sys",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "assistant",
          "content": "a",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "b",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 200,
      "messages": [
        {
          "role": "system",
          "content": "sys",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "assistant",
          "content": "a",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "b",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 0,
      "messages": [
        {
          "role": "system",
          "content": "sys",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "assistant",
          "content": "a",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "b",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 5,
      "messages": [
        {
          "role": "system",
          "content": "sys",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "assistant",
          "content": "a",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "b",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 10,
      "messages": [
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        },
        {
          "role": "system",
          "content": "late system",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 4,
      "messages": [
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 104,
      "messages": [
        {
          "role": "user",
          "content": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          "tool-call-id": null
        },
        {
          "role": "user",
          "content": "hi",
          "tool-call-id": null
        }
      ]
    }
  },
  {
    "fn": "fit",
    "args": {
      "limit": 3,
      "messages": []
    }
  }
]
Versions (units)1›
  1. v1by dave2b363f2d3alatestconfirmed10/23/2026
Verification7›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 12 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 200,000,000 instructions.
  • Already built: a component that passes and uses at most 0.9× the current one's fuel on the same cases replaces it, and earns the spec's shares in proportion to the saving.
  • Found an input where the current component disagrees with the spec? Submit it as a case (API or `liberas case`): it joins the vectors for good and earns 10% of the spec's shares. A component that then passes replaces the broken one for 25%.