Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.vectorshift.ai/llms.txt

Use this file to discover all available pages before exploring further.

What this builds. A small conversation where the user introduces themselves on turn 1 and the agent recalls the details on turn 2. You’ll end up with. Streamed responses for both turns, a confirmation that “Alice” appeared in the second answer, and the final session message count. Demonstrates a multi-turn conversation where the agent remembers context from previous turns within the same session.
import asyncio

from vectorshift.agent import Agent, AgentType, LlmInfo, MemoryConfig
from vectorshift.events import SessionEventType


async def stream_response(session) -> str:
    """Helper: stream one turn and return the full text."""
    full_text = ""
    async for event in session.listen(
        event_types=[
            SessionEventType.MESSAGE_DELTA,
            SessionEventType.MESSAGE_COMPLETE,
        ]
    ):
        if event.delta:
            print(event.delta, end="", flush=True)
            full_text += event.delta
        if event.is_complete:
            full_text = event.text or full_text
            break
    print()
    return full_text


async def main() -> None:
    agent = Agent.new(
        name="Memory demo",
        type=AgentType.CONVERSATIONAL,
        llm_info=LlmInfo(provider="openai", model_id="gpt-4o"),
        tools=[],
        instructions="You are helpful and remember everything the user tells you.",
        memory_config=MemoryConfig(enable_session_memory=True),
    )
    print(f"Created agent: {agent.name}\n")

    async with await agent.create_session() as session:
        # Turn 1
        print("User: My name is Alice and I live in Boston.")
        await session.send("My name is Alice and I live in Boston.")
        print("Assistant: ", end="")
        await stream_response(session)
        print()

        # Turn 2 - the agent should remember the name
        print("User: What is my name and where do I live?")
        await session.send("What is my name and where do I live?")
        print("Assistant: ", end="")
        response = await stream_response(session)
        print()

        # Verify context was retained
        if "Alice" in response:
            print("Context retained across turns!")
        else:
            print("Warning: agent may not have retained context.")

        # Fetch history
        messages = await session.get_messages()
        print(f"\nSession history: {len(messages)} messages")

    agent.delete()
    print("Done.")


if __name__ == "__main__":
    asyncio.run(main())

Expected output

Created agent: Memory demo

User: My name is Alice and I live in Boston.
Assistant: Nice to meet you, Alice...

User: What is my name and where do I live?
Assistant: Your name is Alice and you live in Boston.

Context retained across turns!

Session history: 4 messages
Done.
The second turn proves session memory is working — enable_session_memory=True plus reusing the same session context manager is all it takes.
See Session overview for the full event taxonomy and streaming semantics.

See also

Single-turn session

The one-turn building block this example extends.

Session send_many

Batch context messages with the user question in a single turn.

Agent reference

Full method surface.