R-Think

Cookbook

Common patterns and recipes for building with R-Think. Each recipe solves a specific engineering problem in governed reasoning.

Retry with Backoff

Implement retry logic with exponential backoff for transient failures in provider calls.

retry.ts typescript
const resilientProtocol = Protocol.define({
  id: 'resilient-provider',
  steps: [
    {
      name: 'fetch-data',
      action: fetchFromProvider,
      retry: {
        maxAttempts: 5,
        backoff: 'exponential',
        initialDelay: 1000,
        maxDelay: 30000,
        retryable: ['network_timeout', 'provider_unavailable', 'rate_limited']
      }
    }
  ]
});

Composing Protocols

Compose complex reasoning workflows from smaller protocol units. Each sub-protocol handles a specific cognitive step.

compose.ts typescript
const observationProtocol = Protocol.define({
  id: 'observation',
  steps: [{ name: 'observe', action: ingestSource }]
});

const validationProtocol = Protocol.define({
  id: 'validation',
  steps: [{ name: 'validate', action: validateEvidence }]
});

const decisionProtocol = Protocol.define({
  id: 'decision',
  steps: [{ name: 'decide', action: reachDecision }]
});

const composed = Protocol.compose({
  id: 'full-mission',
  steps: [
    { protocol: observationProtocol },
    { protocol: validationProtocol },
    { protocol: decisionProtocol }
  ]
});

Composition Best Practice

Keep composed protocols idempotent. Each protocol should be able to run independently if needed, and should produce its own traceable artifacts.

Dynamic Rules

Generate rules dynamically based on mission configuration or runtime context.

dynamic-rules.ts typescript
const dynamicRules = (config) => [
  Rule.required('observation'),
  Rule.type('observation', 'string'),
  ...(config.requireSource ? [Rule.required('source')] : []),
  ...(config.maxDepth ? [Rule.bounded('depth', 0, config.maxDepth)] : [])
];

const mission = Runtime.create({
  protocol,
  rules: dynamicRules({ requireSource: true, maxDepth: 3 })
});