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

# Lyzr Cognis + Agno

> Add persistent memory to Agno agents using Lyzr Cognis

## Overview

[Agno](https://docs.agno.com/) is a lightweight framework for building AI agents with tools and structured outputs. By wrapping Cognis methods as Agno tools, you can build agents that autonomously decide when to store, retrieve, and manage their own memories.

**What you'll build:** A personal assistant that uses Cognis-backed tools to remember user preferences, search past conversations, and manage its own memory — all driven by the agent's own reasoning.

**Integration pattern:** Tool-based — Cognis operations exposed as `@tool` decorators. The agent decides when and what to remember.

## Prerequisites

<Tabs>
  <Tab title="Hosted (lyzr-adk)">
    ```bash theme={null}
    pip install lyzr-adk agno openai
    ```

    ```bash theme={null}
    export LYZR_API_KEY="your-lyzr-api-key"
    export OPENAI_API_KEY="your-openai-api-key"
    ```
  </Tab>

  <Tab title="Open Source (lyzr-cognis)">
    ```bash theme={null}
    pip install lyzr-cognis agno openai
    ```

    ```bash theme={null}
    export GEMINI_API_KEY="your-gemini-key"
    export OPENAI_API_KEY="your-openai-api-key"
    ```
  </Tab>
</Tabs>

## Quick Start

<Tabs>
  <Tab title="Hosted (lyzr-adk)">
    ```python theme={null}
    from agno.agent import Agent
    from agno.models.openai import OpenAIChat
    from agno.tools import tool
    from lyzr import Cognis, CognisMessage

    cog = Cognis()

    @tool
    def search_memory(query: str) -> str:
        """Search user's memories for relevant information."""
        results = cog.search(query=query, owner_id="user_123", limit=5)
        if not results:
            return "No relevant memories found."
        return "\n".join(f"- {r.content}" for r in results)

    @tool
    def add_memory(user_message: str, assistant_response: str) -> str:
        """Store important information from the conversation."""
        cog.add(
            messages=[
                CognisMessage(role="user", content=user_message),
                CognisMessage(role="assistant", content=assistant_response),
            ],
            owner_id="user_123",
        )
        return "Memory stored successfully."

    agent = Agent(
        model=OpenAIChat(id="gpt-4o"),
        tools=[search_memory, add_memory],
        instructions=["Store important user info with add_memory",
                       "Search memories before answering personal questions"],
    )

    response = agent.run("My name is Sarah and I love hiking.")
    ```
  </Tab>

  <Tab title="Open Source (lyzr-cognis)">
    ```python theme={null}
    from agno.agent import Agent
    from agno.models.openai import OpenAIChat
    from agno.tools import tool
    from cognis import Cognis

    m = Cognis(owner_id="user_123")

    @tool
    def search_memory(query: str) -> str:
        """Search user's memories for relevant information."""
        resp = m.search(query, limit=5)
        if not resp["results"]:
            return "No relevant memories found."
        return "\n".join(f"- {r['content']}" for r in resp["results"])

    @tool
    def add_memory(user_message: str, assistant_response: str) -> str:
        """Store important information from the conversation."""
        m.add([
            {"role": "user", "content": user_message},
            {"role": "assistant", "content": assistant_response},
        ])
        return "Memory stored successfully."

    agent = Agent(
        model=OpenAIChat(id="gpt-4o"),
        tools=[search_memory, add_memory],
        instructions=["Store important user info with add_memory",
                       "Search memories before answering personal questions"],
    )

    response = agent.run("My name is Sarah and I love hiking.")
    # Don't forget: m.close() when done
    ```
  </Tab>
</Tabs>

## Complete Example: Personal Assistant

### Step 1: Initialize and Define Tools

<Tabs>
  <Tab title="Hosted (lyzr-adk)">
    ```python theme={null}
    import os
    from agno.agent import Agent
    from agno.models.openai import OpenAIChat
    from agno.tools import tool
    from lyzr import Cognis, CognisMessage

    cog = Cognis(api_key=os.getenv("LYZR_API_KEY"))
    OWNER_ID = "user_123"
    AGENT_ID = "assistant_agno"

    @tool
    def search_memory(query: str) -> str:
        """Search the user's memories for relevant information.
        Use this before answering personal questions or when the user
        references something from a past conversation."""
        results = cog.search(query=query, owner_id=OWNER_ID, limit=5)
        if not results:
            return "No relevant memories found."
        formatted = "\n".join(f"- {r.content}" for r in results)
        return f"Found {len(results)} relevant memories:\n{formatted}"

    @tool
    def add_memory(user_message: str, assistant_response: str) -> str:
        """Store important information from the conversation into long-term memory.
        Use this when the user shares personal preferences, important facts,
        or information they'd want remembered."""
        cog.add(
            messages=[
                CognisMessage(role="user", content=user_message),
                CognisMessage(role="assistant", content=assistant_response),
            ],
            owner_id=OWNER_ID,
            agent_id=AGENT_ID,
        )
        return "Memory stored successfully."

    @tool
    def get_all_memories() -> str:
        """Retrieve all stored memories for the current user.
        Use this when the user asks what you remember about them."""
        memory_list = cog.get(owner_id=OWNER_ID, limit=20)
        if not memory_list.memories:
            return "No memories stored yet."
        formatted = "\n".join(
            f"- [{m.id[:8]}] {m.content}" for m in memory_list.memories
        )
        return f"All memories ({len(memory_list.memories)}):\n{formatted}"

    @tool
    def delete_memory(memory_id: str) -> str:
        """Delete a specific memory by its ID.
        Use this when the user asks to forget something."""
        success = cog.delete(memory_id=memory_id, owner_id=OWNER_ID)
        if success:
            return f"Memory {memory_id} deleted successfully."
        return f"Failed to delete memory {memory_id}."
    ```
  </Tab>

  <Tab title="Open Source (lyzr-cognis)">
    ```python theme={null}
    from agno.agent import Agent
    from agno.models.openai import OpenAIChat
    from agno.tools import tool
    from cognis import Cognis

    m = Cognis(owner_id="user_123", agent_id="assistant_agno")

    @tool
    def search_memory(query: str) -> str:
        """Search the user's memories for relevant information.
        Use this before answering personal questions or when the user
        references something from a past conversation."""
        resp = m.search(query, limit=5)
        if not resp["results"]:
            return "No relevant memories found."
        formatted = "\n".join(f"- {r['content']}" for r in resp["results"])
        return f"Found {resp['count']} relevant memories:\n{formatted}"

    @tool
    def add_memory(user_message: str, assistant_response: str) -> str:
        """Store important information from the conversation into long-term memory.
        Use this when the user shares personal preferences, important facts,
        or information they'd want remembered."""
        m.add([
            {"role": "user", "content": user_message},
            {"role": "assistant", "content": assistant_response},
        ])
        return "Memory stored successfully."

    @tool
    def get_all_memories() -> str:
        """Retrieve all stored memories for the current user.
        Use this when the user asks what you remember about them."""
        resp = m.get_all(limit=20)
        if not resp["memories"]:
            return "No memories stored yet."
        formatted = "\n".join(
            f"- [{mem['memory_id'][:8]}] {mem['content']}" for mem in resp["memories"]
        )
        return f"All memories ({resp['total']}):\n{formatted}"

    @tool
    def delete_memory(memory_id: str) -> str:
        """Delete a specific memory by its ID.
        Use this when the user asks to forget something."""
        result = m.delete(memory_id)
        if result["success"]:
            return f"Memory {memory_id} deleted successfully."
        return f"Failed to delete memory {memory_id}."
    ```
  </Tab>
</Tabs>

### Step 2: Create the Agent

```python theme={null}
agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    tools=[search_memory, add_memory, get_all_memories, delete_memory],
    instructions=[
        "You are a helpful personal assistant with persistent memory.",
        "When the user shares important personal information (preferences, "
        "facts about themselves, plans), use add_memory to store it.",
        "Before answering personal questions or when the user references "
        "past conversations, use search_memory to check for relevant memories.",
        "If the user asks what you remember, use get_all_memories.",
        "If the user asks to forget something, use delete_memory.",
    ],
    show_tool_calls=True,
    markdown=True,
)
```

### Step 3: Run Multi-Turn Conversations

```python theme={null}
conversations = [
    "Hi! My name is Sarah and I'm a software engineer. I love hiking and Italian food.",
    "What's a good restaurant recommendation for me?",
    "I'm planning a trip to Colorado next month for hiking.",
    "What do you remember about me?",
    "Actually, I prefer Thai food now instead of Italian.",
]

for user_msg in conversations:
    print(f"User: {user_msg}")
    response = agent.run(user_msg)
    print(f"Assistant: {response.content}\n")
```

The agent will:

1. **Turn 1:** Call `add_memory` to store Sarah's name, job, and preferences
2. **Turn 2:** Call `search_memory` for food preferences, then recommend Italian restaurants
3. **Turn 3:** Call `add_memory` to store the Colorado trip plan
4. **Turn 4:** Call `get_all_memories` to show everything it remembers
5. **Turn 5:** Call `add_memory` with the updated food preference

## Advanced Patterns

### Cross-Session Search

<Note>
  Cross-session search (`cross_session=True`) is a hosted-only feature. Open-source Cognis searches the global `(owner_id, agent_id)` scope by default, which already spans sessions.
</Note>

```python theme={null}
@tool
def search_all_sessions(query: str) -> str:
    """Search across all conversation sessions."""
    results = cog.search(query=query, owner_id=OWNER_ID, cross_session=True, limit=10)
    if not results:
        return "No memories found across any session."
    return "\n".join(f"- {r.content}" for r in results)
```

### Summary-Based Memory

<Note>
  Summaries are a hosted-only feature (`store_summary`, `get_current_summary`, `search_summaries`).
</Note>

```python theme={null}
@tool
def get_session_summary(session_id: str) -> str:
    """Get a summary of a past conversation session."""
    result = cog.get_current_summary(owner_id=OWNER_ID, session_id=session_id)
    return result.get("content", "No summary available for this session.")
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Cognis + CrewAI" icon="users" href="/cognis/cookbooks/cognis-crewai">
    Memory for multi-agent crews
  </Card>

  <Card title="Cognis + LangGraph" icon="diagram-project" href="/cognis/cookbooks/cognis-langgraph">
    Memory as graph nodes
  </Card>
</CardGroup>
