King Klown Logo
King Klown& KOA

Provider Adapter Grok

Architectural Lineage (Credits):
SwarmCraft is an architectural fork and deep rewrite of the multi-agent swarm engine created by Mojomast in mojomast/swarmussy.
SwarmCraft’s deterministic “Architect-style” layering is also derived from the meta-structure of Abstract Wiki Architect (AWA).
Full details: Credits & Lineage

POWERED BY GROK

SwarmCraft is powered by Grok through a dedicated provider adapter layer.

Goal:

This page documents the expected behavior of the Grok adapter, without tying the rest of the codebase to Grok-specific response shapes.


1) Responsibilities of the Provider Adapter

The Grok adapter MUST:

  1. Build requests from SwarmCraft’s internal prompt format.
  2. Send requests to Grok with correct auth + model settings.
  3. Normalize responses into:
    • text output
    • optional structured tool calls
    • token usage and latency metrics (if provided)
  4. Apply robust retry/backoff rules.
  5. Emit consistent errors for the orchestrator to handle deterministically.

The adapter SHOULD:


SwarmCraft should call providers through a stable interface like:

class LLMProvider:
    def generate(self, messages, tools=None, tool_choice=None, **opts) -> ProviderResult:
        ...

Where ProviderResult is normalized:

{
  "text": "string",
  "tool_calls": [
    {
      "name": "write_file",
      "arguments": { "path": "...", "content": "..." }
    }
  ],
  "usage": {
    "input_tokens": 0,
    "output_tokens": 0,
    "total_tokens": 0
  },
  "meta": {
    "model": "grok-...",
    "latency_ms": 0
  }
}

This allows the orchestrator to remain unchanged if you later add other providers.


3.1 Environment variables

Recommended keys (names may vary by implementation):

3.2 Project-level overrides

Optional per-project settings file, e.g.:

Recommended fields:

{
  "llm": {
    "provider": "grok",
    "model": "grok-...",
    "temperature": 0.7,
    "max_tokens": 2000
  }
}

4) Tool Calling and Safety

SwarmCraft personas must not write files directly. They request tool calls.

The Grok adapter MUST support:

Tool execution remains in SwarmCraft (not in Grok):

This preserves deterministic safety rules:


5.1 Retry classes

Adapter SHOULD retry on:

Adapter SHOULD NOT retry blindly on:

5.2 Backoff

Use exponential backoff with jitter.

5.3 Deterministic surfacing

Adapter errors MUST be normalized into stable error codes so the orchestrator can:

Example normalized error:

{
  "error": {
    "code": "PROVIDER_TIMEOUT",
    "message": "Request timed out after 60s",
    "retryable": true
  }
}

6) Token and Cost Tracking (Optional)

If Grok provides usage metadata, the adapter SHOULD emit it in ProviderResult.usage.

If usage is not provided, SwarmCraft MAY estimate tokens separately, but should mark them as estimates.

Token tracking integrates with dashboard/cost panels:


7) How Grok Fits the Deterministic Pipeline

The provider is invoked only during EXECUTE (and optional planning calls if you LLM-assist planning).

The pipeline remains:

Pipeline: Deterministic Pipeline