---
title: Continue a conversation
description: Feed a run's transcript back with weft.Messages plus the next weft.Prompt to continue a conversation, and store the transcript as JSON between requests.
url: https://weftgo.dev/docs/guides/conversations/
type: how-to
sources:
  - weft/core/run.go:649-666 (Prompt, Messages), 689-721 (RunResult.Messages, Text)
  - weft/core/agent.go:602-606 (an Agent is immutable; runs share no state)
  - weft/core/loop.go:234-241 (the loop repairs every run's input, exempting the pending calls a resume decides)
  - weft/core/repair.go:5-16 (Repair, the interrupted-call text)
  - 'weft/core/message.go:8-27 (SchemaVersion, roles, no system role), 72-75 (UnmarshalJSON: an unknown part type is an error)'
  - weft/core/errors.go:182-190 (RunError carries the partial transcript in Result)
  - weft/core/message.go:178-190 (ReasoningPart keeps its signature on the wire)
  - weft/AGENTS.md:168-169 (block 4), rule 1, rule 2
  - weft/example_test.go:775 (ExampleAgent_conversation), 99 (ExampleRepair)
  - weft/docs/adr/0001-message-model.md (transcript invariants)
  - docs-site/examples/conversations/main.go, main_test.go TestConversations, TestAgentHoldsNoHistory, TestRepairedInputIsInTranscript
---

# Continue a conversation

To continue a conversation, pass the previous run's transcript with [`weft.Messages`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Messages) and the new question with [`weft.Prompt`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Prompt). The agent keeps no history: the conversation is the messages you pass, so you store them between requests and send them back.

Two turns, from `conversations/main.go`:

```go title="conversations/main.go"
res, err := agt.Generate(ctx, weft.Prompt("Where is order 1234?"))
if err != nil {
	return err
}
// The next turn: the transcript so far, then the new question.
res2, err := agt.Generate(ctx, weft.Messages(res.Messages...), weft.Prompt("And order 5678?"))
if err != nil {
	return err
}
```

[conversations/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/conversations/main.go)

[`res.Messages`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#RunResult.Messages) is the full transcript, the input messages included. The second run sends the model three messages: the first question, the first answer and the new question. Its own `res2.Messages` holds four, ready for the turn after.

## Feed the transcript back

`weft.Messages` adds existing messages to the run's input; `weft.Prompt` adds one `user` message after them. Options apply in order, so put `Messages` before `Prompt`. The package example [`ExampleAgent_conversation`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#example-Agent-Conversation) is the same two turns.

An [agent](https://weftgo.dev/docs/concepts/agents/) is an immutable value whose runs share no state. One agent can serve every conversation at once; which conversation a run continues depends only on the messages it is given. The system prompt is not part of the transcript, since there is no system role: each turn uses the agent's [`weft.Instructions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Instructions), or a per-run override ([Change an agent's configuration for one run](https://weftgo.dev/docs/guides/run-options/)).

The same pattern resumes a parked run: pass `res.Messages` back with `weft.Approve` or `weft.Deny` ([Require human approval for a tool](https://weftgo.dev/docs/guides/approvals/)). After a failed run, the partial transcript is on the error's `Result` field ([`*weft.RunError`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#RunError)), so you can continue from what the run got done.

## Store the transcript between requests

A [`weft.Message`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Message) is a role plus typed parts, and its JSON encoding is a versioned wire format ([`weft.SchemaVersion`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#SchemaVersion)). Marshal the transcript after each run and unmarshal it before the next:

```go title="conversations/main.go"
// store keeps each conversation's transcript as JSON between requests:
// a database row, a cache entry or a file in a real program.
type store map[string][]byte

func (s store) save(id string, msgs []weft.Message) error {
	b, err := json.Marshal(msgs)
	if err != nil {
		return err
	}
	s[id] = b
	return nil
}

func (s store) load(id string) ([]weft.Message, error) {
	var msgs []weft.Message
	if b, ok := s[id]; ok {
		if err := json.Unmarshal(b, &msgs); err != nil {
			return nil, err
		}
	}
	return msgs, nil
}

// reply answers one request of conversation id.
func reply(ctx context.Context, agt *weft.Agent, s store, id, prompt string) (string, error) {
	history, err := s.load(id)
	if err != nil {
		return "", err
	}
	res, err := agt.Generate(ctx, weft.Messages(history...), weft.Prompt(prompt))
	if err != nil {
		return "", err
	}
	return res.Text(), s.save(id, res.Messages)
}
```

[conversations/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/conversations/main.go)

Each part carries a `type` discriminator, so the messages decode back into the same typed parts, provider reasoning signatures included. An unknown part type is a decode error, never a silent drop. The complete example prints the first stored message: `{"role":"user","content":[{"type":"text","text":"Where is order 1234?"}]}`. The [messages concept page](https://weftgo.dev/docs/concepts/messages/) has the whole format.

To persist a long run step by step instead of at its end, register the [`weft.OnMessages`](https://weftgo.dev/docs/guides/observers/) observer: it receives each batch of messages as it joins the transcript. For durable conversations with a writer lease, queued sends, branches and compaction, use [sessions](https://weftgo.dev/docs/sessions/overview/) (`weft/thread`), which store the transcript for you.

## A fed-back transcript is repaired

The loop repairs the input of every run with the same rule as [`weft.Repair`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Repair): every tool call gets a result, and a result with no call is dropped. The one exception is a resume: the calls the earlier run left pending, which this run decides with `weft.Approve` or `weft.Deny`, are left for the run to resolve itself. A call whose result never arrived, as a crashed process can leave one, gets a visible error result. The model receives `no result recorded: the call was interrupted`, and the repaired message is part of the new run's transcript. The complete example feeds back such a transcript.

## Complete example

`conversations/main.go` runs the three cases against scripted models:

```go title="conversations/main.go"
// Command conversations runs the examples of the guide "Continue a
// conversation": go run ./conversations. Every model is scripted with
// wefttest, so it runs offline; swap in a provider adapter for a real one.
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/weftgo/weft"
	"github.com/weftgo/weft/wefttest"
)

// continueConversation runs two turns of one conversation.
func continueConversation(ctx context.Context, w io.Writer) error {
	model := wefttest.Script(
		wefttest.Say("Order 1234 shipped yesterday."),
		wefttest.Say("Order 5678 is still pending."),
	)
	agt := weft.New(model, weft.Instructions("You are a support agent."))
	res, err := agt.Generate(ctx, weft.Prompt("Where is order 1234?"))
	if err != nil {
		return err
	}
	// The next turn: the transcript so far, then the new question.
	res2, err := agt.Generate(ctx, weft.Messages(res.Messages...), weft.Prompt("And order 5678?"))
	if err != nil {
		return err
	}
	fmt.Fprintln(w, "turn 1:", res.Text())
	fmt.Fprintln(w, "turn 2:", res2.Text())
	fmt.Fprintf(w, "turn 2 sent %d messages; its transcript holds %d\n",
		len(model.LastRequest().Messages), len(res2.Messages))
	return nil
}

// store keeps each conversation's transcript as JSON between requests:
// a database row, a cache entry or a file in a real program.
type store map[string][]byte

func (s store) save(id string, msgs []weft.Message) error {
	b, err := json.Marshal(msgs)
	if err != nil {
		return err
	}
	s[id] = b
	return nil
}

func (s store) load(id string) ([]weft.Message, error) {
	var msgs []weft.Message
	if b, ok := s[id]; ok {
		if err := json.Unmarshal(b, &msgs); err != nil {
			return nil, err
		}
	}
	return msgs, nil
}

// reply answers one request of conversation id.
func reply(ctx context.Context, agt *weft.Agent, s store, id, prompt string) (string, error) {
	history, err := s.load(id)
	if err != nil {
		return "", err
	}
	res, err := agt.Generate(ctx, weft.Messages(history...), weft.Prompt(prompt))
	if err != nil {
		return "", err
	}
	return res.Text(), s.save(id, res.Messages)
}

// stored answers two HTTP-style requests through the store.
func stored(ctx context.Context, w io.Writer) error {
	model := wefttest.Script(
		wefttest.Say("Order 1234 shipped yesterday."),
		wefttest.Say("Order 5678 is still pending."),
	)
	agt := weft.New(model)
	s := store{}
	for _, prompt := range []string{"Where is order 1234?", "And order 5678?"} {
		text, err := reply(ctx, agt, s, "conv-1", prompt)
		if err != nil {
			return err
		}
		fmt.Fprintln(w, "reply:", text)
	}
	msgs, err := s.load("conv-1")
	if err != nil {
		return err
	}
	first, err := json.Marshal(msgs[0])
	if err != nil {
		return err
	}
	fmt.Fprintf(w, "stored %d messages; the first is %s\n", len(msgs), first)
	return nil
}

// repaired feeds back a transcript whose last tool call never got its
// result, as a crashed process leaves it.
func repaired(ctx context.Context, w io.Writer) error {
	model := wefttest.Script(wefttest.Say("The lookup was interrupted; let me try again."))
	agt := weft.New(model)
	interrupted := []weft.Message{
		weft.User("Where is order 1234?"),
		{Role: weft.RoleAssistant, Content: []weft.Part{
			weft.ToolCallPart{ID: "call_1", Name: "lookup", Args: json.RawMessage(`{"order_id":"1234"}`)},
		}},
	}
	if _, err := agt.Generate(ctx, weft.Messages(interrupted...), weft.Prompt("Hello?")); err != nil {
		return err
	}
	for _, m := range model.LastRequest().Messages {
		for _, p := range m.Content {
			if r, ok := p.(weft.ToolResultPart); ok {
				fmt.Fprintf(w, "the model received for %s: %q (error: %t)\n", r.CallID, r.Content, r.IsError)
			}
		}
	}
	return nil
}

func run(ctx context.Context, w io.Writer) error {
	steps := []struct {
		title string
		fn    func(context.Context, io.Writer) error
	}{
		{"continue", continueConversation},
		{"store between requests", stored},
		{"repair", repaired},
	}
	for _, s := range steps {
		fmt.Fprintf(w, "== %s\n", s.title)
		if err := s.fn(ctx, w); err != nil {
			return fmt.Errorf("%s: %w", s.title, err)
		}
	}
	return nil
}

func main() {
	if err := run(context.Background(), os.Stdout); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
```

[conversations/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/conversations/main.go)

[View on GitHub](https://github.com/weftgo/weftgo.dev/tree/main/docs-site/examples/conversations) · run it from `docs-site/examples`:

```sh
go run ./conversations
go test ./conversations
```

```text
== continue
turn 1: Order 1234 shipped yesterday.
turn 2: Order 5678 is still pending.
turn 2 sent 3 messages; its transcript holds 4
== store between requests
reply: Order 1234 shipped yesterday.
reply: Order 5678 is still pending.
stored 4 messages; the first is {"role":"user","content":[{"type":"text","text":"Where is order 1234?"}]}
== repair
the model received for call_1: "no result recorded: the call was interrupted" (error: true)
```

## Best practices & gotchas

- **Store `res.Messages`, not a rebuild from events.** The transcript holds the exact parts, signatures and block boundaries; events are a lossy view of them. (Source: `weft/core/run.go:551-574`, OnMessages godoc)
- **Send the history back every turn.** A run with no `Messages` starts a new conversation; the agent remembers nothing between runs. (Source: `ExampleAgent_conversation`, `TestAgentHoldsNoHistory`)
- **Do not edit stored transcripts by hand to fix them.** The loop repairs what it is given, and the repair is visible to the model. (Source: `weft/core/repair.go:5-16`, AGENTS.md rule 2)
- **Bound what you send, not what you store.** A long history costs tokens on every call; trim the request with [`PrepareStep`](https://weftgo.dev/docs/guides/prepare-step/) and keep the full transcript. (Source: `weft/core/agent.go:154-175`)

## See also

- [Messages, parts and the transcript](https://weftgo.dev/docs/concepts/messages/)
- [What is a session?](https://weftgo.dev/docs/sessions/overview/)
- [Observe a run without changing it](https://weftgo.dev/docs/guides/observers/)
- [Rewrite the request each step (PrepareStep)](https://weftgo.dev/docs/guides/prepare-step/)

Symbols used: [`weft.Messages`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Messages) · [`weft.Prompt`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Prompt) · [`RunResult.Messages`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#RunResult.Messages) · [`RunResult.Text`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#RunResult.Text) · [`weft.Message`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Message) · [`weft.SchemaVersion`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#SchemaVersion) · [`weft.Repair`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Repair) · [`weft.RunError`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#RunError) · [`weft.Instructions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Instructions) · [`weft.OnMessages`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#OnMessages) · [`Agent.Generate`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.Generate) · [`weft.ToolCallPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolCallPart) · [`weft.ToolResultPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolResultPart) · [`wefttest.Script`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/wefttest#Script) · [`ExampleAgent_conversation`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#example-Agent-Conversation)
