Kosha

AI agents pay APIs over x402 on Hedera, and Kosha withholds tax in the same transaction.

Kosha

Created At

ETHGlobal Mumbai

Project Description

Kosha is a tax-native x402 payment gateway on Hedera. AI agents pay APIs in USDC over x402, and Kosha works out the Indian tax on each payment and withholds it in the same Hedera transaction.

The problem: under Indian law, USDC is a virtual digital asset. When an agent pays an Indian API in USDC, the API owner is treated as the buyer of that asset, so the owner must deduct 1% TDS (tax deducted at source) once a payer's yearly total passes ₹10,000, or 20% if the payer has no PAN (Indian tax ID). No agent-payment tool handles this when the payment happens. Every API owner selling to agents would have to track yearly totals per payer and withhold the tax by hand, and mistakes cost interest and penalties.

How Kosha works: an API owner adds Kosha Gate, a few lines of server middleware, so the endpoint answers HTTP 402 and is paid through Kosha. When an agent calls it, Kosha Core (our x402 facilitator) quotes the payment. It looks up the agent's identity, converts the price to rupees, checks this payer and API pair's yearly total, and runs a tax engine whose rules live in a versioned rule pack, so a chartered accountant can review and change them without touching code. The agent's client, Kosha Pay, checks every leg against its owner's spending policy (per-payment and daily limits, yearly budget, allowlist), then signs. Core settles it as one atomic Hedera TransferTransaction: the API receives exactly its price, and the TDS goes to a reserve account the agent owns jointly with Kosha (a 2-of-2 key). Core then writes the decision to Hedera Consensus Service: the amounts, the rate, the rule-pack hash and the pair's running total. Anyone can recompute the tax from that public log. After the tax is paid to the government each month, the reserve is released through a scheduled transaction that the agent co-signs.

Kosha Console is the dashboard for API owners, agent owners and their accountants: live payments, earnings, progress toward the threshold, the TDS ledger, and filing exports.

Every 402 response also offers the standard x402 "exact" scheme, so an unmodified x402 client can still pay through Kosha. We've settled such a payment on testnet.

Why Hedera: fixed fees of a fraction of a cent make it worth withholding 1% of an ₹8 API call. A native multi-party transfer splits the payment atomically without a smart contract. HCS provides an ordered, append-only evidence log that only Kosha can write to. Scheduled transactions handle the jointly controlled reserve without custody code. Kosha uses no smart contracts, only native HTS, HCS and scheduled transactions. Core pays all Hedera fees, so agents and API owners never need HBAR. Agents get an identity through HCS-14 and HCS-11 profiles.

How it's Made

Kosha is a TypeScript monorepo (pnpm workspaces and Turborepo, Node 24) with four parts that ship and three shared packages.

Kosha Core is our own x402 facilitator: a Fastify 5 server with Zod 4 schemas, Postgres through Drizzle ORM, and node-cron jobs. It quotes, verifies and settles payments, runs the tax engine, and is the only component that touches Hedera, as the fee payer. Kosha Gate is middleware an API owner drops into an Express server. Kosha Pay wraps fetch on the agent's side. The Console is Next.js 16 with Tailwind v4, shadcn/ui and TanStack Query. The tax engine is a pure function, decide(), with no I/O, driven by a versioned JSON rule pack. Every wire format is a Zod schema in one shared package, so Core, Gate, Pay and the Console validate against the same contracts.

Hedera is the backbone. We use the Hiero SDK and no smart contracts, only native services:

HTS: one TransferTransaction sends the price to the API and the tax to the agent's reserve, atomically. A contract-free split like this is what makes 1% of an ₹8 API call worth withholding at a fixed fraction-of-a-cent fee. HCS: every tax decision is published to a topic whose submit key is Core's, with the amounts, rate, rule-pack hash and the pair's running yearly total, so anyone can recompute it. Scheduled transactions: each agent's reserve is a 2-of-2 KeyList account (agent plus Kosha). After the monthly tax payment, Core schedules the release and the agent co-signs it, so Kosha never holds an agent's key. Mirror Node: payer keys, reconciling timed-out receipts, and Gate's reserve proof. HCS-14 and HCS-11 give each demo agent a UAID and an on-chain profile. The demo agent runs on Hedera Agent Kit v4 with LangChain. Settlement runs on Circle testnet USDC, plus our own 6-decimal test token for development, because the faucet only gives 20 USDC every two hours. We follow x402 v2 exactly (PAYMENT-REQUIRED, PAYMENT-SIGNATURE, PAYMENT-RESPONSE headers, network hedera:testnet) using @x402/core's codecs.

The interesting parts:

We had to invent our own x402 scheme. The official Hedera "exact" scheme rejects any credit except one to payTo, so a tax split can't fit in it. Every 402 offers two entries: "kosha-split", our atomic split for Kosha Pay, and the standard "exact" one.

Compatibility mode lets a stock x402 client pay a Kosha endpoint unchanged. The client pays the all-in amount to a Kosha settlement account, and a loop in Core forwards the price to the API and the tax to a pooled reserve in a second transaction. We record that transaction's ID before submitting it. If it times out, we ask the Mirror Node what happened instead of resubmitting, so nothing is ever paid twice. We tested this with unmodified @x402/fetch and @x402/hedera.

Gate doesn't take Core's word that the tax was reserved. Before serving the response, it reads the settled transaction from the Mirror Node itself and checks the reserve was credited. It imports a fetch-only Mirror Node module, so the Hedera SDK never enters the API owner's bundle.

Because Core pays every fee, it is what an attacker would target. Its verifier rejects anything that isn't exactly the quoted transfer: wrong fee payer, memo or nodes, a fee cap that's too high, any HBAR, NFT or allowance transfer, an extra credit, or a tax leg that doesn't go to the payer's own reserve. Each failure has its own error code. Pay runs the mirror-image checks before it signs.

To make sure we never under-deduct, a pair's yearly total also counts quotes still open, so two concurrent quotes just under the ₹10,000 threshold can't both skip the tax. Tax is rounded up twice: in rupees, then again in USDC micro-units. Money is always an integer, never a float.

HCS writes go through an outbox stored in the same database transaction as the payment, so a slow consensus write never blocks a payment or loses evidence.

Privacy: no PAN or counterparty name ever reaches HCS. A payer and receiver pair appears only as an HMAC, and a PAN only as a salted commitment in Postgres.

Tests use PGlite, Postgres running in-process, so they need no Docker. The tax engine also has fast-check property tests checking that it never under-deducts and that tax never drops as the amount rises.

background image mobile

Join the mailing list

Get the latest news and updates

Kosha | ETHGlobal