> For the complete documentation index, see [llms.txt](https://docs.minded.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.minded.com/sdk/debugging.md).

# Debugging

MindedJS provides comprehensive debugging capabilities to help you understand and troubleshoot your agent's behavior during development and production. This guide covers various debugging techniques and tools available in the SDK.

## Debug Logging

Set the log level to debug in your environment:

```env
LOG_LEVEL=debug
```

## Debugging Logical Conditions

You can debug and breakpoint on logical conditions during development:

```typescript
// Listen to condition evaluation events
import { AgentEvents } from 'mindedjs';

agent.on(AgentEvents.ON_LOGICAL_CONDITION, async ({ edge, state, condition }) => {
  console.log('[Debug] Evaluating condition:', condition);
  console.log('[Debug] Current memory:', state.memory);
});

agent.on(AgentEvents.ON_LOGICAL_CONDITION_RESULT, async ({ condition, result, executionTimeMs }) => {
  console.log('[Debug] Result:', result);
  console.log('[Debug] Execution time:', executionTimeMs, 'ms');
});
```

## LLM Debug Callback Handler

A good practice is to periodically inspect the actual messages being sent to the LLM to ensure they match your expectations. You can breakpoint and view the final prompt messages after compilation using `LLMDebugCallbackHandler`.

### Using the LLM Debug Callback Handler

The `LLMDebugCallbackHandler` can be imported directly from the main package:

```typescript
import { Agent, LLMDebugCallbackHandler } from '@minded-ai/mindedjs';

// Create the debug handler
const debugHandler = new LLMDebugCallbackHandler();

// Configure your agent with the debug handler
const agent = new Agent({
  config: {
    ...config,
    llm: {
      ...config.llm,
      properties: {
        ...config.llm.properties,
        callbacks: [debugHandler],
      },
    },
  },
  tools,
  memorySchema,
});
```

### Advanced Usage - Custom Debug Handler

You can extend the `LLMDebugCallbackHandler` to add your own debugging logic:

```typescript
import { LLMDebugCallbackHandler } from '@minded-ai/mindedjs';

class CustomDebugHandler extends LLMDebugCallbackHandler {
  async handleLLMEnd(output: any, ...args: any[]): Promise<void> {
    // Log token usage if available
    const tokenUsage = output.llmOutput?.tokenUsage;
    console.log(`Token usage: ${tokenUsage.totalTokens}`);
  }
}
```

The `LLMDebugCallbackHandler` extends LangChain's `BaseCallbackHandler` and can override different mathods in langchain flow. See the [LangChain documentation](https://js.langchain.com/docs/modules/callbacks/).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.minded.com/sdk/debugging.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
