Create a simple agent that maintains conversation history:
Copy
from egregore import Agent# Create an agent with OpenAI's GPT-4agent = Agent(provider="openai:gpt-4")# Make your first callresponse = agent.call("Hello! What's the weather like?")print(response)# The agent maintains context automaticallyresponse = agent.call("What did I just ask you about?")print(response) # Agent remembers the previous question
The agent automatically manages context history using PACT’s depth system. Previous messages are pushed back through ODI (Overlap Demotion Invariant) as new turns are added.
Add persistent memory to your agent with the built-in notes scaffold:
Copy
from egregore import Agentfrom egregore.core.context_scaffolds import InternalNotesScaffold# Create scaffold for persistent notesnotes = InternalNotesScaffold()# Create agent with scaffoldagent = Agent( provider="openai:gpt-4", scaffolds=[notes])# The agent now has access to note-taking toolsresponse = agent.call( "Remember that my favorite color is blue. " "Also, I prefer working in the mornings.")# Later in the conversation...response = agent.call("What do you know about my preferences?")# Agent can retrieve notes from its persistent memory
from egregore import Agentagent = Agent(provider="openai:gpt-4")# Stream the responsefor chunk in agent.stream("Write a poem about AI"): print(chunk, end="", flush=True)
Manipulate context precisely using PACT coordinates:
Copy
from egregore import Agentfrom egregore.core.context_management import TextContentagent = Agent(provider="openai:gpt-4")# Insert a persistent reminder at system depthreminder = TextContent(content="Always be concise and helpful.")agent.context.pact_insert("d-1,0", reminder)# The reminder persists across all turnsresponse = agent.call("Explain quantum computing")# Response will be concise due to the system-level reminder
from egregore import Agentdef get_weather(location: str) -> str: """Get the current weather for a location.""" # In reality, you'd call a weather API return f"The weather in {location} is sunny, 72°F"# Create agent with custom toolagent = Agent( provider="openai:gpt-4", tools=[get_weather])# Agent can now use the weather toolresponse = agent.call("What's the weather in San Francisco?")print(response)
from egregore import Agentagent = Agent(provider="openai:gpt-4")# Add a hook to log all tool calls@agent.hooks.tool.pre_calldef log_tool_call(ctx): print(f"🔧 Calling tool: {ctx.tool_name}") print(f" Parameters: {ctx.parameters}")@agent.hooks.tool.post_calldef log_tool_result(ctx): print(f"✅ Tool result: {ctx.tool_result}")# Tool calls will now be loggedresponse = agent.call("What's the weather in NYC?")
agent = Agent(provider="openai:gpt-4")# Context is automatically managedagent.call("My name is Alice")agent.call("I like Python programming")agent.call("What do you know about me?")# Agent remembers: name and Python preference
from egregore.core.context_management import TextContentagent = Agent(provider="openai:gpt-4")# Add a reminder that expires after 3 turnsreminder = TextContent(content="User is in a hurry", ttl=3)agent.context.pact_insert("d0,1", reminder)# Reminder available for 3 turns, then automatically removed