Docs / Guides

AI agent tool calls and audit logs

audit signs every tool call an AI agent makes and keeps a hash-chained, signed audit log that can be verified later, so tools only run calls that really came from a known agent.

01Sign a tool call

import { audit, keyset } from 'pqlock';

// agent side: sign with the agent's private sign keyset
const call = audit.signCall(agentKeys, { agent: 'billing-agent', tool: 'refund', args: { order: 'A-1009', amount: 40 } });
await fetch(toolUrl, { method: 'POST', body: JSON.stringify(call) });

// tool side: verify before doing anything
const replay = new audit.ReplayCache();
const ok = audit.verifyCall(audit.parseCall(body), {
  keysFor: (agent) => trustedAgents.get(agent),   // public sign keysets by agent id
  replay,
});
call = audit.sign_call(agent_keys, "billing-agent", "refund", {"order": "A-1009", "amount": 40})
call, err := audit.SignCall(agentKeys, "billing-agent", "refund", argsJSON, time.Now())
err = audit.VerifyCall(call, opts)

A call binds the agent id, tool name, SHA-256 of the arguments, a nonce and a timestamp. Verification refuses unknown agents, edited arguments, stale calls (two-minute window by default) and replays. Failures carry a reason: malformed, args, agent, signature, stale or replay.

02Hash-chained audit log

const log = new audit.AuditLog(toolKeys);
log.append({ call: ok, result: 'refunded' });
fs.writeFileSync('audit.log', log.text());       // the whole log so far

const result = audit.verify(publicToolKeys, fs.readFileSync('audit.log'), { length: 1200, head });
log_text = audit.append(log_text, {"call": call, "result": "refunded"}, tool_keys)
result = audit.verify(log_text, public_tool_keys, length=1200, head=head)
log := audit.NewLog(toolKeys)
entry, err := log.Append(eventJSON, time.Now())

Each entry carries a sequence number, a timestamp, the hash of the previous entry and a signature. Editing, deleting or reordering any entry breaks the chain at that point, and verify reports the first bad entry. Keep the latest length and head hash somewhere separate (an anchor) to also detect truncation of the tail.

pql audit verify -k tool.pub --length 1200 --head 9c1f… audit.log

pqlock v0.1.0 · unaudited preview · source of truth: spec/