Composable LLM reasoning patterns
More power than one-off prompts, less weight than a framework. Budget-aware execution with zero runtime dependencies.
pip install executionkit See it in action
Run five LLM completions in parallel, get the consensus answer with cost tracking — in three lines.
import osfrom executionkit import consensus, Provider
provider = Provider( "https://api.openai.com/v1", api_key=os.environ["OPENAI_API_KEY"], model="gpt-4o-mini",)
result = await consensus(provider, "Classify this support ticket: ...", num_samples=5)print(result) # The classificationprint(result.cost) # TokenUsage(input_tokens=250, output_tokens=45, llm_calls=5)print(result.metadata["agreement_ratio"]) # 0.8 — 4 of 5 agreedBuilt for production
Three composable patterns that handle the hard parts of LLM reasoning.
Consensus Voting
Run N completions in parallel, aggregate via majority or unanimous voting. Whitespace-normalized comparison ensures accuracy.
Iterative Refinement
Score-guided improvement loop with convergence detection. Built-in prompt injection defense via XML sandboxing.
Tool-Calling Loop
Think-act-observe ReAct loop with JSON Schema validation. Tool errors become observations, never crash the loop.
Budget Tracking
Per-call and cumulative token usage. Set hard limits with max_cost to prevent runaway spend.
Zero Dependencies
Pure stdlib. Optional httpx for connection pooling. Works anywhere Python 3.11+ runs.
Any Provider
OpenAI, Ollama, Groq, Together AI, GitHub Models — anything speaking the OpenAI-compatible format.
Compose patterns into pipelines
Chain consensus → refine_loop with pipe(). Costs accumulate automatically.
graph LR
A[User Prompt] --> B(consensus)
B --> C(refine_loop)
C --> D(react_loop)
D --> E[PatternResult]
style A fill:#1c2128,stroke:#39d353,color:#e6edf3
style B fill:#1c2128,stroke:#f0883e,color:#e6edf3
style C fill:#1c2128,stroke:#f0883e,color:#e6edf3
style D fill:#1c2128,stroke:#f0883e,color:#e6edf3
style E fill:#1c2128,stroke:#39d353,color:#e6edf3 from executionkit import pipe, consensus, refine_loopfrom functools import partial
result = await pipe( provider, "Explain gradient descent in simple terms.", consensus, partial(refine_loop, target_score=0.9),)
print(result) # Final refined valueprint(result.cost) # Cumulative cost across both stepsWorks everywhere
Any OpenAI-compatible endpoint. Zero config change between providers.