An agent is a loop: the model chooses a tool, your code runs it, and the result goes back to the model. That power comes with new failure modes. Start with one tool, a bounded task and a human approval step for anything that spends money or changes data.
Key takeaways
- Tools should have narrow inputs, explicit side effects and a timeout.
- The agent needs a stop condition, a step limit and a cost limit.
- Log every tool call, input, output and latency; without traces you cannot debug the loop.
- Measure quality on a fixed evaluation set before you optimise the prompt.
What matters in practice
- Use structured outputs and validation for tool arguments.
- Keep irreversible actions behind human approval until the eval set is strong.
How to implement it
- Pick a bounded task. For example: research a company from public pages and draft a summary with sources.
- Define one or two tools. Search, fetch, calculate or query a database. Keep each tool idempotent where possible.
- Add validation and limits. Schema-validate arguments, cap steps, set timeouts and budget.
- Add observability. Trace the prompt, tool calls, results and final answer.
- Create an evaluation set. Include ambiguous, adversarial and no-answer cases.
- Add human approval. Require confirmation before sending email, issuing refunds or changing production data.
A working example
const result = await agent.invoke(
{ input: "Find the official Next.js deployment docs and summarise the self-hosting options." },
{ configurable: { thread_id: "demo" }, recursionLimit: 8 },
);
// Every tool call should be logged with its input, output and duration.
Cost, latency and reliability
| Concern | What drives it | What to do |
|---|---|---|
| Runaway loop | No step or cost limit | Set recursionLimit, token budget and a wall-clock timeout |
| Wrong tool | Vague tool description | Write explicit names, descriptions and examples |
| Unsafe side effect | Agent can send or delete | Require human approval and use least-privilege credentials |
| Hard to debug | No trace | Log prompts, tool calls and outputs with a run ID |
| Quality drift | No eval set | Version the prompt and run a fixed evaluation before release |
Security and privacy
- Keep provider keys on the server and scope them to the minimum permissions.
- Treat retrieved documents and user input as untrusted; separate instructions from data.
- Redact sensitive fields before sending them to a third-party model.
- Log prompts and outputs only as long as you need them, with access controls.
- Provide a human escalation path for high-impact or low-confidence answers.
Common mistakes
- Giving an agent broad database or email permissions on day one.
- Letting it run without a step limit or cost budget.
- Using an agent for a task that a deterministic workflow handles better.
- Skipping traces and then guessing why an answer was wrong.
How to evaluate it
- Build a fixed set. Real questions, ambiguous cases, no-answer cases and adversarial prompts.
- Score the output. Correctness, citation quality, refusal behaviour, latency and cost.
- Review failures. Group them by retrieval, prompt, model or product gap.
- Version changes. Record the prompt, model and retrieval settings with each release.
Evaluation rubric
| Dimension | Pass condition | How to test |
|---|---|---|
| Correctness | Matches the source or is clearly marked as uncertain | Human review against a reference answer |
| Grounding | Every factual claim has a source | Citation coverage and spot checks |
| Refusal | Declines when the context is insufficient | No-answer and adversarial cases |
| Safety | Does not follow injected instructions | Prompt-injection test set |
| Latency | Within the product's interaction budget | p95 on a production-like request |
| Cost | Within the per-task budget | Tokens, retrieval calls and retries |
Rollout plan
- Internal only. Test with the team and a fixed evaluation set; log every failure.
- Small pilot. Expose it to a narrow user group with a visible feedback control.
- Human in the loop. Require review for high-impact answers or actions.
- Measure. Compare task completion, correction rate, latency and cost with the baseline.
- Expand or stop. Expand only if the rubric passes; keep a kill switch and a fallback.
Data governance
| Question | What to decide | Why |
|---|---|---|
| What data is sent? | Minimum fields, redaction and retention | Third-party processing and privacy |
| Where is it stored? | Logs, embeddings and evaluation sets | Access and deletion |
| Who can see it? | Roles, support and vendor access | Least privilege |
| How long is it kept? | Prompts, outputs and feedback | Retention policy |
| How is it deleted? | User, tenant and backup paths | Privacy requests |
| What is the fallback? | Search, form or human handoff | The model will fail sometimes |
Frequently asked questions
No. The AI SDK or a small custom loop can be enough for one or two tools. LangChain helps when you need integrations, memory, tracing and a broader ecosystem.
Least-privilege tools, human approval for irreversible actions, step and cost limits, and a dry-run mode. Treat the agent as an untrusted operator.
Yes, but browsing adds untrusted content and prompt-injection risk. Sanitise retrieved text, restrict domains and never let web content authorise a side effect.
Score task success, tool correctness, citation quality, latency and cost on a fixed set. Include cases where the agent should stop or ask for help.
Sources and further reading
- LangChain.js documentation (js.langchain.com)
- OpenAI API documentation (platform.openai.com)
- OWASP Top 10 for LLM Applications (genai.owasp.org)
Related reading
Planning an AI feature?
Describe the user, the task and the data. I will tell you whether a prompt, RAG or a deterministic workflow is the right starting point.
How to cite this guide: Nur Hossain Repon, “AI agents on the web LangChain Next.js 2026”, Nhrepon Portfolio, reviewed October 2026, https://nhrepon.com/posts/ai-agents-on-the-web-langchain-next-js-2026.





Comments
No comments yet.