Workflows and Document Preprocessing for LLMs in Complex Engineering
Large language models can drift in complex engineering work: they may edit code prematurely or claim a task is complete without compiling it. This article presents three practical controls for LLM-assisted engineering: a staged workflow with build verification, context isolation through subagents, and a preprocessing pipeline for converting technical documents to Markdown. It explains how to define acceptance criteria, enforce real build results, split subtasks at a sensible granularity, and keep the main agent focused on plans and decisions. Each section includes actionable practices and failure-handling considerations for firmware, low-level driver porting, and other complex engineering tasks.
Large language models can easily go off track in complex engineering work. They may start modifying code immediately, or claim that a task is “complete” without compiling it. This article discusses three ways to constrain and support them: a staged workflow with build verification, context isolation through subagents, and a preprocessing pipeline for converting technical documents to Markdown. Each section provides actionable practices and explains what to do when things fail.
1. Constrain the Model with a Staged Workflow
The first problem in complex tasks is that the model is eager to act. The solution is to split a task into three stages and allow the next stage only after the current one is finished.
- Requirement alignment: No code changes are allowed at this stage. The model clarifies ambiguities by asking questions and writes the results as acceptance criteria.
- Specification and plan: The model outputs a technical specification and an implementation plan. The plan must be detailed to the file level, listing new files, modified files, function signatures, and dependencies. The developer confirms it before moving to the next step.
- Step-by-step execution and review: Modify the code step by step according to the plan. After execution, an independent review step compares the actual changes with the plan. Any change beyond the plan must be explained separately.
Acceptance criteria should use a fixed template with at least five items: inputs (original code, data, or documents), outputs (artifacts and paths), constraints (interfaces that must not be changed, memory limits, coding standards), test cases (normal and boundary scenarios listed separately), and definition of done (what counts as finished). The template gives “done” a checkable basis.
Verification Before Completion
In firmware and low-level driver porting, the model often outputs code that cannot compile and then claims that compilation passed. The solution is to make “claiming completion” depend on real execution results. The requirements are as follows:
- Runtime environment: Execute in a sandbox or a controlled local build environment. Do not connect to production devices or flash real hardware.
- Build command: Use the command defined in the project documentation, for example
cmake --build build --target firmware. The model must not replace it with another command on its own. - Timeouts and logs: Set a timeout for each build, for example 30 minutes. Write both standard output and standard error to a log file for the review stage.
- Success criteria: The exit code is 0, the log contains no error-level messages, and the target artifact exists with a timestamp after this build. All three conditions must be met.
- Permission boundaries: The build process may write only to the working directory and must not access the network to download unreviewed dependencies.
Reading only the exit code is not enough. Compilers sometimes return success while the artifact is incomplete, so the decision must check both the logs and the artifacts.
2. Subagent Scheduling and Context Isolation
In large porting tasks, the main conversation quickly fills with code snippets, error logs, and intermediate conclusions. The model can therefore forget the original plan and may break completed logic in later changes. Context isolation can mitigate this problem.
The division of work is as follows: the main agent keeps only the overall plan, progress table, and key decisions; subagents complete specific tasks in independent contexts and return only summaries to the main agent.
Granularity of Subtask Splitting
Granularity directly affects the success rate. Split by module or interface, and a single subtask usually involves only one to three files. A cross-module interface change should be a separate subtask: first complete the interface definition, then dispatch changes to consumers. If granularity is too coarse, the subagent’s context will still expand; if it is too fine, merge costs will rise.
The summary received by the main agent should use fixed fields: task ID, status (passed, failed, needs human intervention), list of modified files, build result, and unresolved issues. With fixed summary fields, the growth of the main context is controllable and comparison is easier.
Scheduling Example
The pseudocode below only illustrates the flow and is not directly runnable. A real implementation needs to add the subagent creation interface, path permissions, and rules for handling merge conflicts.
# Pseudocode example (not directly runnable)plan = make_plan(requirements) # Main agent creates a file-level planfor task in plan.tasks:for attempt in range(MAX_RETRY): # Set a retry limit, e.g., 2child = spawn_subagent(allowed_paths=task.allowed_paths, # Only these paths may be modifiedinputs=task.inputs, # Inject only files needed for this task)result = child.run(task.spec)check = run_build_and_tests(task) # Run by the main workflow, not the subagent's self-reportif check.passed:breakif not check.passed:# Retry or escalate for human review according to the plan...
