> ## Documentation Index
> Fetch the complete documentation index at: https://docs.honeyhive.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain JS

> Reference documentation for the HoneyHiveLangChainTracer class in JS

The `HoneyHiveLangChainTracer` class is a utility designed to trace and log LangChain operations with the HoneyHive API. It extends the `BaseCallbackHandler` from LangChain and provides methods for session management and updating various properties.

## Explanation

The HoneyHiveLangChainTracer is designed to capture and log various events that occur during the execution of LangChain operations. It does this by overriding several "handle" methods from the BaseCallbackHandler class. These methods are automatically called by LangChain at different points during the execution of chains, LLMs, tools, and other components.

You can find the specific implementation of those handler functions in [this file](https://github.com/honeyhiveai/typescript-sdk/blob/main/src/sdk/lc_tracer.ts#L44).

## Constructor

### `constructor(input: HoneyHiveTracerInput)`

Initializes a new instance of the `HoneyHiveLangChainTracer`.

**Parameters**:

* `input: HoneyHiveTracerInput` - An object with the following properties:
  * `project: string` - Name of the project associated with this tracing session.
  * `sessionName: string` - Name for this specific session.
  * `source?: string` - Source identifier (default: 'langchain').
  * `userProperties?: Record<string, any>` - User properties for the session.
  * `metrics?: Record<string, any>` - Initial metrics for the session.
  * `config?: Record<string, any>` - Configuration for the session.
  * `metadata?: Record<string, any>` - Initial metadata for the session.
  * `apiKey?: string` - API key for authenticating with HoneyHive (falls back to `HONEYHIVE_API_KEY` environment variable if not provided).
  * `verbose?: boolean` - Whether to log verbose output (default: false).
  * `baseUrl?: string` - HoneyHive API base URL (default: '[https://api.honeyhive.ai](https://api.honeyhive.ai)').

**Throws**:

* `Error` if the HoneyHive API key is not set.

**Usage Example**:

```typescript theme={null}
const tracer = new HoneyHiveLangChainTracer({
  project: "MyProject",
  sessionName: "TestSession",
  apiKey: "your-api-key-here"
});
```

## Methods

### `async setFeedback(feedback: Record<string, any>): Promise<void>`

Sends feedback to HoneyHive, associating it with the current session.

**Parameters**:

* `feedback: Record<string, any>` - Feedback to be sent to HoneyHive.

**Returns**: `Promise<void>`

**Usage Example**:

```typescript theme={null}
await tracer.setFeedback({ rating: 5, comment: "Excellent response" });
```

### `async setMetric(metrics: Record<string, any>): Promise<void>`

Sends metrics to HoneyHive, associating them with the current session.

**Parameters**:

* `metrics: Record<string, any>` - Dictionary of metrics to be sent to HoneyHive.

**Returns**: `Promise<void>`

**Usage Example**:

```typescript theme={null}
await tracer.setMetric({ response_time: 1.5, accuracy: 0.95 });
```

### `async setMetadata(metadata: Record<string, any>): Promise<void>`

Sends metadata to HoneyHive, associating it with the current session.

**Parameters**:

* `metadata: Record<string, any>` - Dictionary of metadata to be sent to HoneyHive.

**Returns**: `Promise<void>`

**Usage Example**:

```typescript theme={null}
await tracer.setMetadata({ user_id: "12345", context: "customer_support" });
```

### `async setUserProperties(userProperties: Record<string, any>): Promise<void>`

Updates user properties for the current session in HoneyHive.

**Parameters**:

* `userProperties: Record<string, any>` - Dictionary of user properties to be sent to HoneyHive.

**Returns**: `Promise<void>`

**Usage Example**:

```typescript theme={null}
await tracer.setUserProperties({ user_type: "premium", language: "en" });
```

### `async startNewSession(): Promise<void>`

Starts a new tracing session with HoneyHive.

**Returns**: `Promise<void>`

**Notes**:

* This method uses the configuration provided in the constructor to initialize a new session.
* It sets the `sessionId` property of the tracer instance.

**Usage Example**:

```typescript theme={null}
await tracer.startNewSession();
```

## Error Handling

All methods log errors to the console if they fail. If `verbose` is set to `true` in the constructor, additional error details will be logged.

## Notes

* The `HoneyHiveLangChainTracer` class automatically traces various LangChain operations (LLM calls, chain executions, tool usage, etc.) and sends the traces to HoneyHive.
* Make sure to call `startNewSession()` before using other methods if you want to explicitly start a new session. Otherwise, the session will be implicitly started when the first trace is sent.
* The `sessionId` is automatically generated and managed by the tracer. You don't need to provide or manage it manually.
