# Agent history and chat memory

Agent history and memory are two similar but distinct concepts within AI agents. For more details on the exact difference, Langchain4J does a good job explaining the difference simply here: <https://docs.langchain4j.dev/tutorials/chat-memory#memory-vs-history>&#x20;

The Talent Agent SDK implements both memory and history for you.  The history is managed automatically behind the scenes and is stored forever.  It is what powers the built-in search API.  ChatMemory is also handled automatically with a built-in Spring component but can be overridden if you want it handled differently.

{% hint style="info" %}
Viewing the agent history is an important part of improving your agents during initial deploys.  You should ensure that any terms of service you have with your users and/or customers gives you access to view the AI responses for the purpose of improving their output.
{% endhint %}

### Agent chat memory

Every agent that you build gets the built-in chat memory auto configured.  This memory keeps a rolling 200 messages per conversation and by default the memory ID is managed automatically as you chat back and forth with the agents.

This window can be configured in `application.properties` through the setting:

```properties
peoplelogic.agent.memory.window=50
```

{% hint style="info" %}
By default - the chat memory is not persistent through restarts.  Future versions of the SDK will include an option to enable a memory stored in the database.
{% endhint %}

You can choose to create your own replacement of the ChatMemoryProvider interface and add it to your agents.  The `@PeoplelogicAgent` annotation includes a `chatMemoryProvider` parameter that you can then point to your newly created Spring component.

For more details - check out Langchain4J's great documentation on implementing your own ChatMemory at <https://docs.langchain4j.dev/tutorials/chat-memory>

### Agent chat history

History of the interactions with your agents is handled transparently behind the scenes.  You can access this history several ways:

1. Calling the web API for search (if using the web).
2. Using the CLI to search (if using the CLI).
3. Autowiring `PeoplelogicAgentUtils`  and calling its search method.
4. Autowiring `AgentHistoryRepository` and accessing the history directly.

All of these will yield the same results.  Each message in the history is attached to a conversation and knows whether it was a user message or an agent response. If it was an agent response it also includes the token usage.

In addition to searching the agent history, one potential usage of the direct history might be to use it to break out the themes of queries that users are asking your agent to answer.  You can send the anonymized user messages back to an agent within a tool to get the themes and then output those as the result of the tool.  Or you could use the history to automatically improve the way that an agent responds to inputs.  Feel free to get in touch if you'd like help implementing these ideas!


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.peoplelogic.dev/guides/getting-started-with-the-talent-agent-sdk/agent-history-and-chat-memory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
