Skip to main content
This guide demonstrates how to integrate C1 Visualize API with your existing agentic application using a two-step architecture. This pattern allows you to leverage any LLM provider (OpenAI, Anthropic, etc.) for tool-calling and business logic, while using C1 exclusively for generating beautiful, interactive UI components.

Overview

The two-step Visualize pattern separates concerns between your primary LLM and the UI generation layer:
  1. Step 1 - Business Logic: Your primary LLM handles user requests, executes tool calls, and generates a final text/markdown response
  2. Step 2 - UI Generation: C1 Visualize API converts the text response into interactive, generative UI components
This approach is ideal when:
  • You want to use your existing LLM infrastructure for tool-calling
  • You need to maintain complete conversation history with your primary LLM
  • You want to add beautiful UI generation without refactoring your agent logic
For most applications, we recommend using C1 as the Gateway LLM instead, which provides lower latency and better context awareness.

Architecture

Key Concepts

Message Types

The pattern uses two distinct message storage strategies: AIMessage: Complete conversation history with your primary LLM
  • Includes user prompts, assistant responses, tool calls and their results
  • Essential for maintaining context in subsequent LLM calls
UIMessage: Display-optimized messages for the frontend
  • User prompts and final generative UI responses from C1
  • Used for rendering the chat interface

The Two-Step Flow

  1. Step 1: Call your primary LLM with full conversation context, allowing it to use tools and generate a complete response
  2. Step 2: Send the final LLM response to C1 Visualize API, which transforms it into rich UI components without needing tool access

Setup

1. Install Dependencies

npm

2. Environment Variables

Create a .env file with your API keys:
You can create a new API key from Developer Console

3. Configure OpenAI Clients

Create two OpenAI client instances - one for your standard LLM calls and one for C1 Visualize:
TypeScript/Next.js

Implementation

Backend API Route

Create an API route that orchestrates the two-step process:
app/api/chat/route.ts

Frontend Component

Use the GenUI SDK to render the chat interface:
app/page.tsx

System Prompt Best Practices

When crafting your system prompt for the primary LLM, include guidance for the downstream visualizer:
  1. Mention the Visualizer: Inform the LLM that its output will be processed by a UI generator
  2. Provide Context: Explain that the visualizer doesn’t have access to tools or database
  3. Request Structure: Ask for actionable elements like buttons, forms, and links
  4. Format Hints: Suggest specific UI components when appropriate (tables, charts, etc.)

Example System Prompt

Message Persistence

Store two types of messages with distinct purposes: AIMessage Table
  • Stores complete LLM conversation history including tool calls and results
  • Used for context in subsequent LLM calls
UIMessage Table
  • Stores user-facing messages with generative UI responses
  • Used for rendering chat interface

Example Prisma Schema

schema.prisma

Troubleshooting

Enhance your system prompt to provide more context about expected UI patterns and include relevant metadata in the assistant’s final message.
For development, you can truncate to recent messages. For production, use prompt caching with conversation compaction:

Best Practices

  1. Separate Concerns: Keep business logic in your primary LLM and UI generation in C1
  2. Rich Context: Provide detailed information in the final assistant message for better UI generation
  3. Actionable Elements: Always include next-step actions (buttons, forms) in your responses
  4. Message Storage: Store both AI and UI message histories for optimal context management
  5. Error Recovery: Implement graceful fallbacks if either API call fails
  6. Testing: Test the full two-step flow with various query types and edge cases
This pattern introduces additional latency since you must wait for the primary LLM to complete before C1 can start streaming. For most applications, consider using C1 as the Gateway LLM instead.

Full Example on GitHub

See a complete working implementation of this pattern in our e-commerce agent example with tools, database integration, and production-ready code.