Inference-time reasoning optimization using QUBO/Ising formulations
Project description
SpinChain
Inference-time reasoning optimization for LLMs using QUBO/Ising formulations. Built as an MCP server for Claude Code.
SpinChain takes multiple diverse reasoning chains, extracts fragments, formulates their selection as a QUBO problem (following QCR-LLM), solves it with simulated annealing, and returns the most stable and coherent fragment subset.
Installation
Prerequisites
- Python 3.11+
- uv (recommended) or pip
Install skills (recommended)
Install SpinChain skills into Claude Code via the plugin marketplace:
/plugin marketplace add ameyakhot/spinchain
/plugin install spinchain-skills@spinchain
This gives you 6 skills: spinchain-optimize, analyze-spinchain, test-spinchain, spinchain-status, spinchain-bench, spinchain-trace.
Clone and install the server
git clone https://github.com/ameyakhot/spinchain.git
cd spinchain
uv sync
To install with dev dependencies (pytest, ruff):
uv sync --extra dev
Register as an MCP server in Claude Code
Add SpinChain to your Claude Code config (~/.claude.json):
{
"mcpServers": {
"spinchain": {
"type": "stdio",
"command": "uv",
"args": [
"run",
"--directory",
"/path/to/spinchain",
"python",
"-m",
"spinchain"
],
"env": {
"SPINCHAIN_TRACE_DIR": "/path/to/spinchain/traces"
}
}
}
}
Replace /path/to/spinchain with the actual path to your cloned repo.
Restart Claude Code. The optimize_reasoning tool will be available as mcp__spinchain__optimize_reasoning.
How it works
User asks a complex reasoning question
│
▼
Claude generates N diverse reasoning chains
│
▼
Claude calls optimize_reasoning(completions=[...])
│
▼
┌─────────────────────────────────────────────┐
│ SpinChain Pipeline │
│ │
│ 1. Fragment Extraction │
│ Split into sentences, embed with │
│ sentence-transformers, deduplicate │
│ by cosine similarity │
│ │
│ 2. QUBO Formulation (QCR-LLM) │
│ Linear: -μ·popularity + α·risk │
│ Quadratic: -β·(correlation - λ²·sim) │
│ │
│ 3. Simulated Annealing (dwave-neal) │
│ 100 reads × 1000 sweeps │
│ │
│ 4. Stability Ranking │
│ Select fragments appearing in ≥50% │
│ of low-energy solutions │
└─────────────────────────────────────────────┘
│
▼
Claude receives optimized fragments
and synthesizes final answer
Usage
As an MCP server (primary use)
Once registered, Claude calls SpinChain automatically when using the optimize_reasoning tool. No manual invocation needed.
Standalone example
Run the bundled example with synthetic reasoning chains (no API key needed):
uv run python examples/basic_usage.py
Trace analysis
Every MCP call is logged to SPINCHAIN_TRACE_DIR/spinchain_traces.jsonl. Analyze with:
# Human-readable report
uv run python -m spinchain.analyze
# JSON output
uv run python -m spinchain.analyze --json
# Last 10 calls only
uv run python -m spinchain.analyze --last 10
Run tests
uv run --extra dev python -m pytest tests/ -v
Skills
Install via /plugin marketplace add ameyakhot/spinchain in Claude Code, or use as slash commands when working inside the project directory.
| Skill | Description |
|---|---|
spinchain-optimize |
Core skill — teaches Claude how to generate diverse chains and call optimize_reasoning effectively |
analyze-spinchain |
Run trace analysis — usage stats, latency breakdown, anomalies |
test-spinchain |
Run the full test suite (71 tests) |
spinchain-status |
Health check — config, imports, trace summary |
spinchain-bench |
Run the example benchmark and inspect pipeline output |
spinchain-trace |
Inspect raw trace records — recent calls, specific trace IDs |
Project structure
spinchain/
├── src/spinchain/
│ ├── server.py # MCP server — optimize_reasoning tool
│ ├── tracing.py # JSONL trace logger
│ ├── analyze.py # Trace analysis CLI
│ ├── formulation/
│ │ ├── fragment_extractor.py # Sentence splitting, embedding, dedup
│ │ ├── coefficient_builder.py # QCR-LLM linear + quadratic weights
│ │ └── qubo_builder.py # BQM construction + cardinality constraint
│ └── solvers/
│ ├── base.py # Abstract solver interface
│ └── simulated_annealing.py # dwave-neal SA solver
├── tests/ # 71 tests
├── examples/
│ └── basic_usage.py # Synthetic benchmark
├── .claude/commands/ # Claude Code slash commands
└── traces/ # Trace logs (created at runtime)
Configuration
Environment variables
| Variable | Default | Description |
|---|---|---|
SPINCHAIN_TRACE_DIR |
~/.spinchain/traces/ |
Directory for JSONL trace logs |
optimize_reasoning parameters
| Parameter | Default | Description |
|---|---|---|
completions |
required | List of reasoning chain strings (min 2) |
num_reads |
100 | SA samples per solve |
num_sweeps |
1000 | MC sweeps per sample |
similarity_threshold |
0.85 | Cosine similarity for fragment dedup |
selection_threshold |
0.25 | Fraction of low-energy solutions for ranking |
inclusion_threshold |
0.50 | Min frequency to include a fragment |
cardinality_k |
None | Target number of fragments to select |
QUBO hyperparameters (in code)
| Parameter | Default | Description |
|---|---|---|
mu |
1.0 | Fragment popularity weight |
alpha |
0.5 | Risk/variance penalty |
beta |
1.0 | Pairwise coherence weight |
lambda_sim |
0.3 | Semantic similarity penalty factor |
penalty_strength |
5.0 | Cardinality constraint weight |
Solver roadmap
- Simulated Annealing (current) — dwave-neal, pure Python
- COBI Ising chip — millisecond solves, hardware accelerated
- Quanfluence CIM — optical coherent Ising machine
- IBM Quantum QAOA — gate-based quantum validation
All solvers share the same BaseSolver interface and QUBO formulation.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spinchain-0.1.2.tar.gz.
File metadata
- Download URL: spinchain-0.1.2.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21da5addf334a8dd0d57bd0576dc7c183f7410c386842c43f6235e5398d346c5
|
|
| MD5 |
824d66c02413a46b12669e8d192ece6d
|
|
| BLAKE2b-256 |
fa0f5f97e6c26848891764140631ec33d7ea656689943ff653ab328327f36031
|
Provenance
The following attestation bundles were made for spinchain-0.1.2.tar.gz:
Publisher:
publish.yml on ameyakhot/spinchain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spinchain-0.1.2.tar.gz -
Subject digest:
21da5addf334a8dd0d57bd0576dc7c183f7410c386842c43f6235e5398d346c5 - Sigstore transparency entry: 1286127736
- Sigstore integration time:
-
Permalink:
ameyakhot/spinchain@c740c29c368478e2b17ed3234cc17e09655a2407 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/ameyakhot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c740c29c368478e2b17ed3234cc17e09655a2407 -
Trigger Event:
release
-
Statement type:
File details
Details for the file spinchain-0.1.2-py3-none-any.whl.
File metadata
- Download URL: spinchain-0.1.2-py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14e18e8bd4139a5bfc313f75e524e9bc32dffd33727f9034b0a087637635913e
|
|
| MD5 |
a4741f077ce77d59225d877c1986af81
|
|
| BLAKE2b-256 |
ae25ec5c1f61d7297e4828a8f70a179d3bba13aec25387634c14e69799fd0d3f
|
Provenance
The following attestation bundles were made for spinchain-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on ameyakhot/spinchain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spinchain-0.1.2-py3-none-any.whl -
Subject digest:
14e18e8bd4139a5bfc313f75e524e9bc32dffd33727f9034b0a087637635913e - Sigstore transparency entry: 1286127870
- Sigstore integration time:
-
Permalink:
ameyakhot/spinchain@c740c29c368478e2b17ed3234cc17e09655a2407 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/ameyakhot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c740c29c368478e2b17ed3234cc17e09655a2407 -
Trigger Event:
release
-
Statement type: