Source: raw/Build_Your_Own_Claude_Code_Full_AI_Coding_Agent_Tutorial.md — multi-chapter live-coding bootcamp tutorial, youtube.com/watch?v=k_D_C3ExypU, fetched 2026-05-20.

A reference architecture for “Night Code,” a Claude Code / OpenCode alternative built from scratch as a multi-chapter tutorial. Covers the agent harness layer + the SaaS business layer: provider-agnostic LLM dispatch via the Vercel AI SDK, OpenTUI React-based terminal UI, Hono backend, plan-mode vs build-mode tool gating, OAuth browser-to-CLI authentication (via Clerk), credits-based billing (via Polar), preview deploys (Railway), AI code review (CodeRabbit), and error monitoring (Sentry). Reproducible — the architecture is itself what’s useful, not “build Claude Code from scratch” theory.

Key Takeaways

  • Night Code is positioned explicitly as a Claude Code / OpenCode alternative. Multi-package monorepo: terminal UI + backend server + shared types + database layer. Looks visually like OpenCode (declared inspiration, not a fork). Two modes: Build (full read/write/edit/bash tool access) and Plan (read-only — no write, no edit, no shell). Mode-toggling = tool gating. “You control which tools the agent gets and that changes everything about how it behaves.”
  • Four-package monorepo architecture.
    • CLI / Terminal UI — Built with OpenTUI (React bindings for terminal UIs — components / hooks / state, same syntax as web React). Author’s framing: “Your existing React knowledge transfers directly.”
    • Backend ServerHono (lightweight TypeScript framework, Express alternative). Streams LLM responses via server-sent events with live reasoning display.
    • Shared Types / Schemas — TypeScript types both sides share.
    • Database Layer — Sessions, conversations, tool calls, credits.
  • Each tool is its own module. Bash / EditFile / Grep / etc. Each module ships independently. The author claims this matches the unit-of-modularity Claude Code uses internally.
  • Plan-mode vs Build-mode = tool gating. Different modes expose different tool surfaces to the agent:
    • Build Mode: read + write + edit + execute. “It can read, write, edit, and execute commands in your project.”
    • Plan Mode: read + search only. “In plan mode, the agent has no write tools, no edit tools, and no shell access. It can only read and search.”
    • Author’s extension thesis: “Once you understand how this works, you can create your own modes. A review mode, a test mode, a documentation mode.”
  • Provider-agnostic via Vercel AI SDK. Six default models out of the box. “The Vercel AI SDK under the hood means you can plug in Google, Mistral, Grok, or any other provider.” Models switchable mid-session: “I can ask one question with Haiku, the next with Opus, and the third with GPT — all in one session.” The tutorial teaches how to add a new provider yourself.
  • Browser-to-CLI authentication via Clerk OAuth. Most CLI tools paste an API key. Night Code does a proper OAuth flow: login opens your browser → Clerk auth → session returned to terminal automatically. No copy-pasting tokens. Author flags this as a rarely-explained pattern. Pairs with [[claude-ai/cli-reference|Claude Code’s own claude auth login flow]] (W18 added paste-code fallback for WSL2/SSH/containers).
  • Credits-based billing via Polar. Night Code is built as a full SaaS — every request costs credits based on model + token usage. “If you have ever wanted to monetize a CLI tool, sell inference for your own LLM, or ship a paid developer product, this will teach you exactly how to do it.” Polar handles metering + credit system + checkout. Auto-renewal, balance display via usage command, top-up via upgrade command. No webhooks, no Stripe glue code.
  • Session persistence. sessions command shows all past conversations: every message, every tool call, every file change. Click into one and resume. Single source of truth = database layer, not in-memory.
  • CLI theming. theme command picks a color scheme, applies instantly across the whole UI. OpenTUI handles the rendering.
  • Real engineering practices baked in:
    • Git workflow — commits, branches, real PRs per chapter (one chapter = one branch).
    • AI code review via CodeRabbit — every PR auto-reviewed for logic issues, best practices, critical mistakes.
    • Preview deploys via Railway — every PR gets a preview deployment.
    • Error + log monitoring via Sentry — production observability layer.
    • The tutorial walks through actually using these in a working CI pipeline, not just “in theory.”
  • Chapter structure (from grep of the transcript). ~10+ chapters covering: (1) Monorepo + OpenTUI bootstrap → (2) Component architecture (App, Header, InputBar, TextArea, StatusBar, CommandMenu) → (3) Routing + slash commands → (4) Server / Hono → (5+) Tool development, mode toggling, billing, auth, deploy, monitoring. Each chapter is a Git branch.
  • The “I will teach you how I think while building” pitch. Author offers a paid cohort with the value-add being the messy parts that get cut from YouTube tutorials — debugging, refactoring, architecture changes, dead ends, technical decisions. The YouTube version is the polished step-by-step; the cohort is the “watch me think” version. Distinction worth noting: tutorials usually skip the failure-recovery half of real engineering.
  • The “build your own Claude Code” framing is misleading — it’s actually “ship your own AI-coding SaaS using the AI SDK.” This is closer in spirit to a Theo / @t3dotgg full-stack walkthrough than to a “implement a coding agent from scratch” architecture talk. The harness layer is thin (Vercel AI SDK does the heavy lifting); the commercial stack around it is what gets the most chapter time (Polar billing, Clerk OAuth, Railway deploys, Sentry observability).

The build-it-yourself stack

┌──────────────────────────────────────────────────────────┐
│  TERMINAL UI (OpenTUI React)                             │
│  App / Header / InputBar / CommandMenu / Themes          │
│  Streams via SSE                                         │
└─────────────────────┬────────────────────────────────────┘
                      │ HTTP + SSE
                      ▼
┌──────────────────────────────────────────────────────────┐
│  HONO BACKEND                                            │
│  • Tool dispatch (bash, edit_file, grep, etc.)           │
│  • Vercel AI SDK → provider-agnostic LLM call            │
│  • Mode-based tool gating (plan / build / review / ...)  │
│  • SSE streams reasoning + tool calls to UI              │
└──┬───────────────────────────────────────┬───────────────┘
   │                                       │
   ▼                                       ▼
┌────────────────┐                  ┌────────────────────┐
│  DATABASE      │                  │  PROVIDERS         │
│  Sessions      │                  │  Anthropic / OpenAI│
│  Conversations │                  │  Google / Mistral  │
│  Credits       │                  │  Grok / Local      │
└────────────────┘                  └────────────────────┘
   │
   ▼
┌──────────────────────────────────────────────────────────┐
│  SAAS LAYER                                              │
│  • Polar (credits-based billing, checkout, metering)     │
│  • Clerk (OAuth, browser-to-CLI auth)                    │
│  • Railway (preview deploys per PR)                      │
│  • Sentry (error monitoring + logs)                      │
│  • CodeRabbit (AI code review on every PR)               │
└──────────────────────────────────────────────────────────┘

Practical reuse — when this article is the right reference

  • You want to ship a coding-agent SaaS. This is the most current reference architecture in the wiki for that goal (combines OpenTUI + Hono + Polar + Clerk + Vercel AI SDK + Railway + Sentry into one stack).
  • You want to add a new LLM provider to an existing agent harness. The Vercel AI SDK section is the pattern.
  • You want to build mode-based tool gating (plan vs. build vs. review). The “modes are just tool subsets” abstraction is reusable across any harness.
  • You want to do OAuth browser-to-CLI instead of pasting API keys. The Clerk pattern transfers to other CLI tools.
  • You want credits-based metering for any developer tool. The Polar integration is reusable.

When this article is NOT the right reference

  • You want to understand how Claude Code internally works. This builds an alternative, not a clone. Architecture is the author’s choice, not Anthropic’s.
  • You want to learn the loop-with-verification pattern itself. See Sid Benesaria’s verification-loop talk for the loop ratchet.
  • You’re an operator, not a builder. This is a multi-chapter bootcamp for developers. If you’re building agentic workflows in claude.ai or Cowork, this is the wrong layer.

Try It

  • Watch the first 3 chapters for free. Bootstrap monorepo + OpenTUI + component architecture. ~3 hours. By the end you have a working terminal UI.
  • Skip to the tool-dispatch + SSE chapter if you only care about the harness. The billing/auth/deploy chapters are independent.
  • Use the architecture as a reference even if you don’t follow the tutorial. The package boundaries + mode-gating pattern + Vercel-AI-SDK indirection are the durable design choices.
  • Compare with Printing Press CLI Factory for an alternative “generate a CLI from API spec” approach if you’re focused on tooling rather than agentic harness.
  • Check the cohort offering if you want to learn the “real engineering” half the YouTube version skips — debugging / refactoring / architecture-change live walkthroughs.

Open Questions

  • Open-source license — the transcript doesn’t say whether Night Code itself is open-source or proprietary (the cohort gating suggests at least the cohort-track materials are paid). Worth checking the channel + project description after fetching.
  • Author identity — the transcript references “how I solve errors, refactor code, how I make technical decisions, and how I think” but doesn’t surface a name. Most likely a known YouTuber in the tutorial space. Worth WebFetching the YouTube channel handle.
  • Actual chapter count — grep of the transcript shows 10+ chapter references; exact count + names would help shape a “skip to chapter N for X” recommendation.
  • Token-economy of the SaaS layer — credits-based billing only works if the markup over Vercel-AI-SDK passthrough cost is meaningful. The transcript doesn’t disclose the unit economics. Open question for any operator considering shipping their own.
  • Real-world adoption — is Night Code itself published as a downloadable tool, or is it only the tutorial artifact? Worth checking.
  • Differentiator vs OpenCode — author says “this is not a fork” but the visual inspiration is explicit. What features does Night Code add or omit relative to OpenCode? Worth a side-by-side if user wants to actually pick one.