Skip to main content

hdp-grok

HDP (Human Delegation Provenance) middleware for Grok / xAI API — attach a cryptographic audit trail to any Grok conversation with a few lines of code.

Every tool call Grok makes on behalf of a human is recorded in a tamper-evident chain of Ed25519 signatures, verifiable offline with a single public key.

pip install hdp-grok

Quick start

import json
import os
from openai import OpenAI
from hdp_grok import HdpMiddleware, get_hdp_tools

client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.x.ai/v1",
)

middleware = HdpMiddleware(
    signing_key=os.getenv("HDP_SIGNING_KEY"),  # base64url Ed25519 private key
    principal_id="user@example.com",
)

messages = [{"role": "user", "content": "Please issue an HDP token and extend the chain to sub-agent-1."}]

while True:
    response = client.chat.completions.create(
        model="grok-3",
        messages=messages,
        tools=get_hdp_tools(),
    )
    choice = response.choices[0]

    if choice.finish_reason == "tool_calls":
        messages.append(choice.message)
        for tc in choice.message.tool_calls:
            result = middleware.handle_tool_call(
                name=tc.function.name,
                args=json.loads(tc.function.arguments),
            )
            messages.append({"role": "tool", "tool_call_id": tc.id, "content": json.dumps(result)})
    else:
        print(choice.message.content)
        break

How it works

hdp-grok exposes three tool schemas that Grok can call during a conversation:

Tool What it does
hdp_issue_token Signs a root HDP token for the current session and principal
hdp_extend_chain Appends a signed delegation hop to the chain (e.g. handing off to a sub-agent)
hdp_verify_token Verifies the full token chain using the middleware's public key

HdpMiddleware holds the session state — signing key, current token, hop counter — for the lifetime of a conversation. Pass get_hdp_tools() to tools= once; route every hdp_* tool call through middleware.handle_tool_call().


Generating a signing key

from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
import base64

key = Ed25519PrivateKey.generate()
raw = key.private_bytes_raw()
print(base64.urlsafe_b64encode(raw).rstrip(b"=").decode())
# → store this as HDP_SIGNING_KEY

API reference

HdpMiddleware

HdpMiddleware(
    signing_key: bytes | str | None = None,
    # bytes  — raw 32-byte Ed25519 private key
    # str    — base64url or hex (with or without 0x prefix)
    # None   — reads HDP_SIGNING_KEY env var
    key_id: str = "default",
    default_expires_in: int = 3600,   # seconds
    session_id: str | None = None,    # auto UUID v4 if None
    principal_id: str | None = None,
)
Method Returns Notes
issue_token(scope, expires_in) {"token": "<json>"} Raises ValueError if principal_id not set
extend_chain(delegatee_id, additional_scope) {"new_token": "<json>"} Raises HdpTokenMissingError if no root token has been issued
verify_token(token_str) {"valid": bool, "hop_count": int, ...} valid covers integrity; includes recorded_after_period and integrity_violations
handle_tool_call(name, args) dict Routes any hdp_* tool call; handles camelCase from Grok
export_current_token() dict | None Live in-memory token
reset() None Clears token and hop counter; keeps session

get_hdp_tools() → list[dict]

Returns the three OpenAI-compatible tool schemas ready to pass to tools=.

HDP tokens are records and cannot gate actions. Expiry does not affect valid; the legacy expired field remains for compatibility. recorded_after_period lists hop sequence numbers whose timestamps are at or after expires_at. HdpTokenExpiredError remains importable but is deprecated and never raised. hdp_verify_token is an audit operation and must not be used to condition an action.


Wire format compatibility

Tokens produced by hdp-grok use the same Ed25519 + RFC 8785 wire format as the TypeScript @helixar_ai/hdp SDK. A token issued in Python is verifiable in TypeScript and vice versa.


Specification

This package follows draft-helixar-hdp-agentic-delegation-03 (latest revision).

License

Apache-2.0

Metadata

Release files for hdp-grok 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hdp-grok 0.2.0
File Size Uploaded
hdp_grok-0.2.0.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hdp-grok 0.2.0
File Interpreter ABI Platform
hdp_grok-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.6 kB

Release files / hdp_grok-0.2.0.tar.gz

Download URL hdp_grok-0.2.0.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ae16c7288f30d68c9f2495bc0c2b6b9e95c3a342fc788d06909bc0a0c94ec977
BLAKE2b-256 checksum
How to use checksums
507974735e90f2e1fa94e9a3ec2a45e7ba2ea9d0a53adff722ca92c27417f7e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 7, 2026.

Transparency log

Release files / hdp_grok-0.2.0-py3-none-any.whl

Download URL hdp_grok-0.2.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
217105d99f7e7a939dd0e25733ede8463d01deb0160522a3824919e871070722
BLAKE2b-256 checksum
How to use checksums
6bbfa484e7a2cdad41b32364d30dbac24d6d9073d843371f9a25c4b91e5a10eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 7, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page