This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
OpenGradient Python SDK - A decentralized model management and inference platform SDK. The SDK enables programmatic access to model repositories and decentralized AI infrastructure, including end-to-end verified AI execution. Uses uv for dependency management.
# Install dependencies (syncs from pyproject.toml/uv.lock)
make install # or: uv sync --all-extras
# Build distribution
make build
# Publish to PyPI (requires credentials)
make publish# Run all tests
make test
# Run unit tests only
make utils_test
# Run integration tests
make integrationtest# Run formatting (ruff, line-length=140) and type checking (mypy)
make check
# Generate documentation
make docs# Test model inference
make infer
# Test LLM completion
make completion
# Test chat functionality
make chat
# Test streaming chat
make chat-stream
# Test tool calling
make chat-tool-
Client (
src/opengradient/client/): Client package with submodulesllm.py— LLM chat/completion with TEE and x402 payment supportmodel_hub.py— Model repository management (CRUD, upload)twins.py— Digital twins chat integration (twin.fun)alpha.py— Alpha Testnet on-chain inference and workflowsopg_token.py— OPG token Permit2 approvaltee_connection.py— TEE connection management (static and registry-based)tee_registry.py— TEE endpoint discovery via on-chain registry_conversions.py— Model input/output type conversions_utils.py— Shared utilities (ABI loading, retry logic)
-
CLI (
src/opengradient/cli.py): Command-line interface using Click- Commands:
config,infer,completion,chat - Supports file-based input for messages and tools
- Commands:
-
Agents (
src/opengradient/agents/): Framework integrationsog_langchain.py— LangChain ChatModel adapter
-
Blockchain Integration:
- Smart contract ABIs in
src/opengradient/abi/ - Web3 integration for decentralized inference
- Support for TEE (Trusted Execution Environment) mode
- Smart contract ABIs in
-
AlphaSense (
src/opengradient/alphasense/): LangChain-compatible tools for AI agentsrun_model_tool.py— Tool for running on-chain model inferenceread_workflow_tool.py— Tool for reading workflow results
-
Workflow Models (
src/opengradient/workflow_models/): Hardcoded price/volatility forecast models for on-chain execution
- Inference Modes: VANILLA, TEE (end-to-end verified), ZKML
- Model CID: Content-addressed model identifiers
- Dual Authentication: Model Hub (email/password) + blockchain (private key)
- x402 Payments: Streamed micropayment protocol for LLM inference
- Digital Twins: Chat integration with twin.fun personas
- Dual Chain: Base Sepolia (LLM) + OpenGradient testnet (Alpha on-chain inference)
The SDK uses environment-based configuration with defaults:
DEFAULT_RPC_URL: Blockchain RPC endpointDEFAULT_API_URL: Model Hub API endpointDEFAULT_INFERENCE_CONTRACT_ADDRESS: Smart contract address
User configuration stored via opengradient config init wizard.
- Unit tests in
tests/using pytest:utils_test.py,client_test.py,langchain_adapter_test.py,opg_token_test.py - Integration tests:
integrationtest/for agent and workflow models - Stress tests:
stresstest/for load testing LLM and inference endpoints - CLI command testing via Makefile targets
- Run individual suites:
make client_test,make langchain_adapter_test,make opg_token_test
Docs are generated with pdoc3 using a custom Mako template at templates/text.mako. Run make docs to regenerate into docs/. Do not edit generated documentation files in docs/ by hand.
There are concrete example scripts using the SDK in the examples/ folder that highlight how to use the SDK and provides a starting point for developers. Make sure all example files are referenced in the readme in the folder.
There are more detailed walkthroughs and tutorials in the tutorials/ folder.
To link to another module or class from a docstring, use fully qualified names in backticks:
"""
- **`opengradient.client.llm`** -- description here
- **`opengradient.client.onchain_inference`** -- description here
"""The template's linkify function automatically converts `opengradient.x.y.z` references into relative markdown links. Always use fully qualified names starting with opengradient. — never hardcode relative URLs in docstrings.
- Use Google-style docstrings (Args, Returns, Raises, Note, Attributes sections)
- The template parses these sections and renders them as structured markdown
- For classes, Args in the
__init__docstring are rendered under the Constructor heading
Key dependencies (Python >=3.11):
web3ð-account: Blockchain interactionlangchain&openai: LLM integrationsclick: CLI frameworkfirebase-rest-api: Backend servicespydantic: Data validationog-x402: x402 payment protocolnumpy: Array handling