Greenflash TypeScript SDK Integration Plan
Your task is to integrate the Greenflash TypeScript SDK into this codebase and ship the minimal, correct implementation to start sending conversation data. Follow the steps below carefully and exactly, and make sure to use the checklist at the end to ensure you’ve followed all the steps. Important: Before implementing, analyze the codebase to understand:- Where does user input get received vs. where LLM responses are generated?
- Are they in the same function/location or separate?
1) Installation
Install the Greenflash package:2) Configuration
-
Ensure the API key is available at runtime:
- Add to your environment (or
.envfile): - Do not hard-code the key.
- The SDK will automatically read
GREENFLASH_API_KEYfrom environment variables if not passed explicitly.
- Add to your environment (or
-
Ensure you have your Product ID(s):
- Find your Product ID in Greenflash for the product you want to track.
- You will pass this directly to each API call (not as an env var), since a single codebase may serve multiple products.
3) Create a single reusable Greenflash client
Best practice: create one module that exports a ready-to-use client. Name itgreenflash-client.ts(or.js). Keep all env lookups centralized here.
src/lib/greenflash-client.ts or app/services/greenflash-client.ts).
Import client wherever you log messages or identify users/orgs.
4) Log messages with client.messages.create (required fields only)
Required fields:
productIdexternalUserIdexternalConversationIdmessages→ array of{ role: "user" | "assistant" | "system", content: string }
model— the AI model used (e.g.,"gpt-4o","claude-sonnet-4-20250514"). Enables model performance comparison in Greenflash.externalOrganizationId— links conversations to an organization/account for segment analysis.properties— object of custom metadata to attach to the conversation.
Fire-and-forget logging to avoid blocking
Use promises withoutawait to avoid blocking your LLM response:
Note: The .catch() is optional but recommended to prevent unhandled promise rejections from crashing your app.
4A) Where to call it in your flow — choosing the right pattern
The Greenflash messages API is flexible: You can send 1 to many messages per call. Subsequent calls are automatically ordered by timestamp, so you can safely send:- Each message individually (user message, then assistant message separately)
- Messages grouped as turns (user + assistant together in one call)
- An entire conversation at once (less common for real-time logging)
-
Analyze your codebase structure first:
- Look for where user input is received and where the assistant/LLM response is generated
- Check if these happen in the same function or in different parts of the code
-
Pattern A - Send as turns:
- Use if: User input and assistant output are available together in the same function/location
- Example: Your LLM handler receives user input, generates a response, and returns both
- Benefit: One API call per turn, simpler tracking
-
Pattern B - Send individually:
- Use if: User input and assistant output are handled in separate functions/locations
- Example: User input handler is separate from the LLM response generation
- Benefit: Log as events happen, no need to refactor code structure
- Greenflash automatically orders messages by timestamp across calls, so both patterns work correctly
- Choose the pattern that requires minimal changes to the existing codebase
- You can mix patterns if needed (e.g., some conversations logged as turns, others as individual messages)
5) Minimal examples for message payload
6) (Optional) Agentic Workflows
If the product is an AI agent with tool calls, reasoning steps, or multi-step workflows, you can use structuredmessageType values instead of simple role to give Greenflash richer visibility into agent behavior.
Key concepts:
- Use
messageTypeinstead ofroleto describe what the agent is doing - Use
contentonly for user-facing input/output - Use
outputfor internal agent data (thoughts, tool results) - Use
inputfor tool call parameters - Link related messages with
externalMessageIdandparentExternalMessageId
For more details: See the Agentic Messages prompt for comprehensive guidance on message types,contentvsoutput, and modeling complex agent execution.
7) (Optional) Identify users — only the required field
Call once when a user logs in or as soon as a stable ID is available. Required:externalUserId.
Optional fields you can include: externalOrganizationId, name, email, phone, properties (object for custom data).
8) (Optional) Identify organizations — only the required field
Use when you have a clear organization/account ID. Required:externalOrganizationId.
Optional fields you can include: name, properties (object for custom data).
9) Error handling (recommended for production)
While fire-and-forget logging is appropriate for most cases, you may want to handle errors for monitoring purposes:APIConnectionError, RateLimitError, APIError, AuthenticationError, PermissionDeniedError, NotFoundError, ValidationError.
10) File/PR checklist
- Installed package:
npm install greenflash(or yarn/pnpm equivalent) - Added
GREENFLASH_API_KEYto runtime env (not hard-coded). - Have your Product ID ready (from Greenflash) to pass directly in each API call.
- Created
greenflash-client.tswith the client. - Analyzed codebase to determine message logging pattern:
- User input + assistant output in same location → use Pattern A (send as turns)
- User input + assistant output in separate locations → use Pattern B (send individually)
- Logging messages with only required fields:
productId,externalUserId,externalConversationId,messages. - Passing
productIddirectly in eachclient.messages.create(...)call (not from an env var). - Using fire-and-forget pattern (no
await) to avoid blocking LLM responses. - Added
.catch()handlers to prevent unhandled promise rejections. - (Optional) Added
client.users.create(...)where a stable user ID exists. - (Optional) Added
client.organizations.create(...)where an org ID exists.
greenflash-client.ts, minimal logging wired into the main chat flow (using the appropriate pattern based on codebase structure), and optional user/org identification hooks. Include README notes describing env vars and where the logging occurs.
