Long-程 AI Agent Tasks: Closing the Loop from Extraction to Visualization in Three Hours
This article explains how to deliver long-horizon AI agent tasks reliably by turning goals into verifiable constraints. Using ontology-based knowledge graph extraction and visualization as the example, it covers goal definition across data, storage, and interaction layers, machine-readable ontology modeling, extraction and ingestion strategies that control uncertainty, and visualization with source traceability. It also describes the real division of labor in a roughly three-hour, ~800K-token run, plus production guidance on observability, cost control, data security, and regression testing.
1. Why Long-Horizon Tasks Are Hard to Deliver in One Pass
In real engineering work, a “long-horizon task” is not defined by the number of lines of code. It is defined by a long chain of steps, many dependencies, and slow feedback. Take ontology-based knowledge graph extraction and visualization as an example. It spans at least four stages: understanding the ontology constraints, extracting entities and relations from unstructured text, writing the results into a graph database, and providing an interactive query and visualization interface. If any stage falls short, the final system is unusable.
The traditional approach makes the “plan-generate-evaluate” loop explicit in scaffolding: a human breaks down the task, generation and testing capabilities are called separately, and failed cases are aggregated manually at the end. The problem is that context gets fragmented, the model cannot see the whole picture, and errors often surface only at the end of the chain, making rework expensive. When the model itself has strong long-horizon execution and self-evaluation capabilities, the loop can be consolidated into a single context, and the human only needs to provide a clear goal and acceptance criteria.
2. Task Definition: Write the Goal as Verifiable Constraints
Whether a long-horizon task converges reliably depends first on whether the goal is verifiable. It helps to split the goal into three layers:
- Data layer: the input is a batch of unstructured text, and the output is triples that conform to the ontology constraints.
- Storage layer: the triples are written into a graph database, with typed nodes and edges carrying attributes and source-provenance fields.
- Interaction layer: provide a query interface and a visualization view that support filtering by type and drilling down by relation.
Also define the acceptance criteria explicitly, for example entity type coverage, sampled accuracy of relation extraction, the entity deduplication strategy, and query response availability. The more verifiable the goal, the more reliable the automated evaluation that follows.
A schematic ontology constraint can be expressed like this:
{"entity_types": ["Work", "Person", "Organization"],"relation_types": [{"name": "directed_by", "from": "Person", "to": "Work"},{"name": "acted_in", "from": "Person", "to": "Work"}],"constraints": ["Entity names must be normalized", "Every edge must carry the source text snippet"]}
3. Ontology Modeling: Give the Graph the Ability to Improve Itself
The value of an ontology is not just “defining fields” but enabling the graph to validate and evolve. Three points matter in practice:
- Constraints first. Write types, relation directions, and required attributes into a machine-readable constraint file, and use it as the source of truth for both extraction and ingestion.
- Traceability. Keep the original snippet and position on every edge to support sampled review and error attribution.
- Evolvability. Version the ontology, and keep backward-compatibility strategies for existing data when adding types or relations, avoiding a full rebuild.
When the constraints are machine-readable, the evaluator can automatically check for issues such as “illegal edges exist” or “source is missing.” This turns quality control from manual spot checks into sustainable automated validation.
4. Extraction and Ingestion: Containing the Uncertainty
The core tension in extraction is the conflict between the model’s generative uncertainty and the ontology’s hard constraints. A common engineering approach combines several strategies:
- Prompt layer: inject the ontology constraints and a few examples into the context, and require structured output.
- Validation layer: run schema validation on the output, and reject and retry illegal types or directions outright.
- Normalization layer: normalize synonymous entities to reduce graph fragmentation.
- Ingestion layer: write to the graph database in batches, using unique constraints and idempotent writes so that repeated runs do not produce dirty data.
Schematic validation logic:
def validate(triples, ontology):ok = []for t in triples:if t["relation"] not in ontology["relation_types"]:continueif not t.get("evidence"):continueok.append(t)return ok
5. Visualization and Query: Making Results Verifiable by Humans
Visualization is not decoration; it is the entry point for quality inspection. A minimum viable form includes a force-directed graph colored by entity type, clicking a node to expand its attributes, filtering by relation type, and jumping from a source snippet back to its context. On the query side, combined filtering by type and relation is enough for most verification needs. The key here is to expose provenance information in the interface so reviewers can quickly judge whether the extraction is correct.
6. Human-AI Collaboration: The Real Division of Labor in a Three-Hour Loop
In one complete run, an AI coding tool with long-horizon execution capability delivered the full chain from extraction to display in about three hours, consuming close to 800,000 tokens. Its working style was close to a generator and evaluator combined: write code, run tests, read failure messages, change the code, and loop forward until acceptance is passed.
The human role therefore narrows to three things: define a verifiable goal, set the acceptance criteria, and do sampled review at key checkpoints. It is worth stressing that an automated loop does not mean no one is responsible; sampled review and boundary testing before release still cannot be skipped.
7. Production Recommendations: From Usable to Maintainable
To actually put this into production, you still need engineering fundamentals:
- Observability: connect logging services and monitoring alerts to track extraction success rate, ingestion failure rate, and query latency.
- Cost control: cache and batch long-context calls to avoid repeated consumption.
- Data security: store text and graph data in tiers, and desensitize sensitive information before it enters the context.
- Regression mechanism: fix an evaluation set, and run automatic regression after ontology or prompt changes to prevent capability regression.
8. Conclusion
The barrier to long-horizon tasks is shifting from “can you build scaffolding” to “can you define the goal and acceptance clearly.” When the model itself has a cohesive loop of planning, generation, and evaluation, the engineering focus should move forward to goal definition, ontology constraints, and quality control. Get these three things solid, and complex systems can be delivered reliably within controllable cost and keep evolving.
