Claude Example
This page explains how engineers can use examples/claude_analysis.py in OpenTremor-core to run a client-led analysis workflow with Claude and the REST API. Claude chooses which tool to call next; the script makes the HTTP calls. The server does not orchestrate the workflow.
No MCP client is needed. The script exposes REST operations to Claude as tools. This is one example of the client-led pattern, which works with other models too. See Adapting for other models.
How it works
Section titled “How it works”sequenceDiagram
participant Script as User Script
participant Claude as Claude API
participant API as REST API
Script->>Claude: system prompt + changeset
Claude->>Script: ingest
Script->>API: POST /v1/python-code-change/{changeset}/ingest (diff file)
loop until status is "done"
Claude->>Script: claim_units
Script->>API: POST /v1/{changeset}/claims (known_rulesets and known_contract added by the script)
Note over Claude: analyse each unit against its rules
Claude->>Script: submit_analyses, once per batch
Script->>API: POST /v1/{changeset}/analyses
end
Claude->>Script: get_report
Script->>API: GET /v1/changesets/{changeset}/report
Claude-->>Script: final report text
Note over Script: print report
Claude chooses which tools to call and in what order. The script makes the HTTP calls and returns each result to Claude as a tool_result.
The diff does not go through the model. Claude’s ingest tool call names the pack and changeset. The script sends the diff file it already holds. Claude receives a few units at a time from claim_units. The script tracks the ruleset IDs and contract ID it has already received, then sends them as known_rulesets and known_contract on later claims so the server omits their text. Large diffs therefore use no extra model tokens and cannot be truncated when the model copies them.
Prerequisites
Section titled “Prerequisites”pip install anthropic httpxStart the server from the workspace root (see Quick Start):
make upEvery pack starts disabled. In the dashboard: Rules → Packs. Enable python-code-change for the organization.
Set your Anthropic API key and save a branch diff, then run the script:
export ANTHROPIC_API_KEY=your-key-here
# Review the Python changes of a branch (defaults: claude-opus-5-5, http://localhost:8000)git diff origin/main -- '*.py' > change.diffpython examples/claude_analysis.py change.diffEnvironment variables
Section titled “Environment variables”| Variable | Default | Description |
|---|---|---|
ANTHROPIC_API_KEY | — | Anthropic API key. Not needed if you are logged in with ant auth login |
ANTHROPIC_MODEL | claude-opus-5-5 | Any Claude model with tool use |
OPENTREMOR_BASE_URL | http://localhost:8000 | OpenTremor server base URL |
OPENTREMOR_API_KEY | (empty) | OpenTremor API key, if the server requires one. A pipeline key is enough. |
Example — remote server
Section titled “Example — remote server”export ANTHROPIC_API_KEY=sk-ant-...export OPENTREMOR_BASE_URL=https://opentremor.example.comexport OPENTREMOR_API_KEY=my-server-key
python examples/claude_analysis.py change.diffExample output
Section titled “Example output”Changeset : a3f1c2d4e5b6c7d8e9f0...Diff file : change.diffModel : claude-opus-5-5API base : http://localhost:8000
→ ingest(pack_name='python-code-change', changeset='a3f1c2...') → claim_units(changeset='a3f1c2...') → submit_analyses(changeset='a3f1c2...', analyses=[{'unit_hash': 'd4e5f6...', 'verdict': 'Invoice export takes the tenant from the query string...'}, {'unit_hash': 'b7c8d9...', 'verdict': 'Database password committed in settings.py...'}]) → claim_units(changeset='a3f1c2...') # status "done" → loop ends → get_report(changeset='a3f1c2...')
# 🤖 Analysis Report...Script structure
Section titled “Script structure”Tool handlers (build_tool_handlers) wrap REST calls with one httpx.Client. Each returns the HTTP status code and response body so Claude can tell whether a call succeeded.
Tool schemas (TOOLS) define name, description, and input_schema. They follow the REST API request contract so Claude can build a valid submit_analyses payload.
System prompt (SYSTEM_PROMPT) defines the workflow order, the loop’s end condition (status is done), and the finding format.
Agent loop (run) calls client.beta.messages.create() and branches on stop_reason:
stop_reason | What the script does |
|---|---|
tool_use | Runs each tool_use block, returns all results in one user message, then loops |
end_turn | Prints the final text (the report) and exits |
anything else (max_tokens, refusal, …) | Stops with an error |
A few request settings to know:
- Adaptive thinking — Claude decides how much to reason per turn. The assistant turn, including thinking blocks, is sent back unchanged on the next request.
- Prompt caching —
cache_control={"type": "ephemeral"}. The script resends the full conversation each turn. Caching means each turn pays full price only for content added since the previous turn. - Refusal fallback —
fallbacks="default"(betaserver-side-fallback-2026-07-01). If a safety classifier declines a request, the API retries it on Anthropic’s recommended fallback model within the same call. This is available on the Claude API, but not on Amazon Bedrock, Vertex AI, or Microsoft Foundry.
If a tool call fails because an argument is missing or the server is unreachable, the script returns a tool_result with is_error: true to Claude. Claude can then correct the call without the script crashing.
Adapting for other models
Section titled “Adapting for other models”Any model with function or tool calling can use the same REST contract. The SDK call and the format of tool calls and results change.
OpenAI-compatible (Ollama, vLLM, Mistral, …)
Section titled “OpenAI-compatible (Ollama, vLLM, Mistral, …)”from openai import OpenAIclient = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")response = client.chat.completions.create(model="llama3", messages=messages, tools=tools)- Wrap tool schemas as
{"type": "function", "function": {"name", "description", "parameters"}}. Theparametersfield holds the same data asinput_schema. - Tool calls arrive in
response.choices[0].message.tool_calls, withargumentsas a JSON string. - Send each result back in its own
{"role": "tool", "tool_call_id": ..., "content": ...}message.
build_tool_handlers and the REST calls need no changes.