TypeScript Manual Logger#
Logging calls to custom models is supported via the Helicone NodeJS SDK.
To get started, install the `@helicone/helpers` package
Set `HELICONE_API_KEY` as an environment variable
Create a new HeliconeManualLogger instance
Log your request
Install dependencies
Set up the logger and OpenAI client
Using logSingleRequest for non-streaming responses
Using logSingleStream for streaming responses
Install dependencies
Create an API route handler with Together AI
Important note about the `after` function
The after function allows you to perform operations after the response has been sent to the client. This is crucial for logging operations as it ensures they don't delay the response to the user.
When using this approach:
- Logging happens asynchronously after the response is sent
- The user experience isn't affected by logging latency
- You still capture all the necessary data for observability
This is especially important for streaming responses where any delay would be noticeable to the user.
API Reference#
HeliconeManualLogger#
HeliconeLogBuilder#
The HeliconeLogBuilder provides a simplified way to handle streaming LLM responses with better error handling and async support. It's created using the logBuilder method of HeliconeManualLogger.
Methods#
setError(error: any): Sets an error that occurred during the requesttoReadableStream<T>(stream: Stream<T>): Collects streaming responses and converts them to a readable stream while capturing the response for loggingsetResponse(body: string): Sets the response body for non-streaming responsessendLog(): Sends the log to Helicone
logRequest#
Parameters#
request:HeliconeLogRequest- The request object to log
operation:(resultRecorder: HeliconeResultRecorder) => Promise<T>- The operation to be executed and logged
additionalHeaders:Record<string, string>- Additional headers to be sent with the request
- This can be used to use features like session management, custom properties, etc.
Available Methods#
The HeliconeManualLogger class provides several methods for logging different types of requests and responses. Here's a comprehensive overview of each method:
logRequest#
Used for logging non-streaming requests and responses with full control over the operation.
Parameters:
request: The request object to logoperation: A function that performs the actual API call and records the resultsadditionalHeaders: Optional additional headers to include with the log request
Example:
logStream#
Used for logging streaming operations with full control over stream handling.
Parameters:
request: The request object to logoperation: A function that performs the streaming API call and attaches the stream to the recorderadditionalHeaders: Optional additional headers to include with the log request
Example:
logSingleStream#
A simplified method for logging a single ReadableStream without needing to manage the operation.
Parameters:
request: The request object to logstream: The ReadableStream to consume and logadditionalHeaders: Optional additional headers to include with the log request
Example:
logSingleRequest#
Used for logging a single request with a response body without needing to manage the operation.
Parameters:
request: The request object to logbody: The response body as a stringadditionalHeaders: Optional additional headers to include with the log request
Example:
logBuilder#
The recommended method for handling streaming responses with better error handling and simplified workflow.
Parameters:
request: The request object to logadditionalHeaders: Optional additional headers to include with the log request
Example:
Streaming Examples#
Using the Async Stream Parser#
Helicone provides an asynchronous stream parser for efficient handling of streamed responses. This is particularly useful when working with custom integrations that support streaming.
Here's an example of how to use the async stream parser with a custom integration:
The async stream parser offers several benefits:
- Processes stream chunks asynchronously for better performance
- Reduces latency when handling large streamed responses
- Provides more reliable token counting for streamed content
Using Vercel's after Function with Streaming#
When building applications with Next.js App Router on Vercel, you can use the after function to log streaming responses without blocking the client response:
For a comprehensive guide on using the Manual Logger with streaming functionality, check out our Manual Logger with Streaming cookbook.
