Skip to content
DashboardWebsite

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.


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.


Terminal window
pip install anthropic httpx

Start the server from the workspace root (see Quick Start):

Terminal window
make up

Every 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:

Terminal window
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.diff
python examples/claude_analysis.py change.diff
VariableDefaultDescription
ANTHROPIC_API_KEY—Anthropic API key. Not needed if you are logged in with ant auth login
ANTHROPIC_MODELclaude-opus-5-5Any Claude model with tool use
OPENTREMOR_BASE_URLhttp://localhost:8000OpenTremor server base URL
OPENTREMOR_API_KEY(empty)OpenTremor API key, if the server requires one. A pipeline key is enough.
Terminal window
export ANTHROPIC_API_KEY=sk-ant-...
export OPENTREMOR_BASE_URL=https://opentremor.example.com
export OPENTREMOR_API_KEY=my-server-key
python examples/claude_analysis.py change.diff

Changeset : a3f1c2d4e5b6c7d8e9f0...
Diff file : change.diff
Model : claude-opus-5-5
API 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
...

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_reasonWhat the script does
tool_useRuns each tool_use block, returns all results in one user message, then loops
end_turnPrints 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" (beta server-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.


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 OpenAI
client = 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"}}. The parameters field holds the same data as input_schema.
  • Tool calls arrive in response.choices[0].message.tool_calls, with arguments as 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.