Skip to main content

HDP (Human Delegation Provenance) middleware for Grok / xAI API

Project description

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 / HdpTokenExpiredError
verify_token(token_str) {"valid": bool, "hop_count": int, ...} Uses public key derived at init
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=.


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.


License

CC-BY-4.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hdp_grok-0.1.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hdp_grok-0.1.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file hdp_grok-0.1.1.tar.gz.

File metadata

  • Download URL: hdp_grok-0.1.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hdp_grok-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8cb74d7dfc06aa5c1f00f956dd098cccc0224178c16d6ae94e51d121e919137c
MD5 bed783730c06bf75c27de13910362ff0
BLAKE2b-256 76b16596b6c968cd4313c7397b3b83be9444c6d5b987af6b5d6f564f6e00bfe6

See more details on using hashes here.

Provenance

The following attestation bundles were made for hdp_grok-0.1.1.tar.gz:

Publisher: release.yml on Helixar-AI/HDP

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hdp_grok-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hdp_grok-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hdp_grok-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f323a0160d7cf278f1077e56539b06e11d03c1f3fe0f9a66fae0845a349ce10e
MD5 9dd0d6abb5fce1c1b3b4690f3d7f9e4b
BLAKE2b-256 856577c940a80a607d6beb0c41e77a1dfd1d52f18e0165d83556d2f439a87cd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for hdp_grok-0.1.1-py3-none-any.whl:

Publisher: release.yml on Helixar-AI/HDP

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page