i need to add tool calls history also
# ask-ai
b
i need to add tool calls history also
t
Thanks — I see you want to restart your agent from chat history and also include the history of tool calls so the agent retains both conversation and tool-interaction context. That’s the right approach for preserving full state. Here’s how you can do it. Quick summary of steps • Persist both the chat messages and the tool call records before shutting down the agent. • Reconstruct a ChatContext with messages and include the tool-call entries in the same timeline. • Initialize the new agent (or update the existing one) with that restored context so the agent can reason with both conversation and tool history. Concrete approach (concept + Python-style example) 1) Persist history before shutdown • Save conversation messages (role, content, timestamp). • Save tool calls (tool name, input, output/result, timestamp, any metadata). Store them in order (by timestamp or sequence index) so you can reconstruct the exact timeline. 2) Recreate ChatContext and tool-call records • Rebuild your ChatContext by replaying messages in order. • Recreate tool-call entries and attach them to the context or to the agent’s tool history structure so the agent can reference them. Example pattern:
Copy code
# Load the saved timeline (mixed messages + tool calls) in chronological order
saved_timeline = [
    {"type": "message", "role": "user", "content": "What's the weather?"},
    {"type": "tool_call", "tool": "weather_api", "input": {"loc": "NYC"}, "output": {"temp": 58}},
    {"type": "message", "role": "assistant", "content": "It's 58°F in NYC."},
    # ...
]

restored_ctx = ChatContext()

for entry in saved_timeline:
    if entry["type"] == "message":
        restored_ctx.add_message(role=entry["role"], content=entry["content"])
    elif entry["type"] == "tool_call":
        # Represent tool calls in whatever format your agent expects.
        # Example: attach as a special message or a tool record
        tool_record = {
            "tool": entry["tool"],
            "input": entry["input"],
            "output": entry["output"],
            "meta": entry.get("meta")
        }
        # Option A: Add as a structured system message (so it remains in chat history)
        restored_ctx.add_message(
            role="system",
            content=f"[tool_call] {tool_record}"
        )
        # Option B: If your agent has a tool history API, add to that instead:
        # restored_agent_tool_history.add(tool_record)
3) Start or update the agent with restored context • Start a fresh agent with
chat_ctx=restored_ctx
, or call
update_agent()
and pass the restored context/tool history.
Copy code
await session.start(
    agent=MyAgent(chat_ctx=restored_ctx),
    room=ctx.room
)
Design choices and tips • How you represent tool calls matters: ◦ If your agent inspects only chat messages, add tool calls as structured system messages (clear tags like
[tool_call]
) so the assistant can read them. ◦ If your agent supports a separate tool history structure, populate that so tool-call metadata stays distinct from regular chat. • Preserve metadata: timestamps, tool version, and outputs are useful for correct reasoning and for reproducing or debugging previous tool results. • Ordering: keep the exact chronological order so the agent can interpret which tool outputs correspond to which user prompts. • Token management: for large histories, consider summarizing older sections (both messages and tool outputs) while retaining critical tool call details. If you want, tell me: • The shape of your saved tool records (an example JSON entry), and • Whether your agent supports a separate tool-history API or only chat messages, and I’ll produce a tailored code snippet that matches your implementation.