Some gamma-ray bursts (GRBs) have a tera-electron volt (TeV) afterglow, but the early onset of this has not been observed. We report observations with the Large High Altitude Air Shower Observatory (LHAASO) of the bright GRB 221009A, which serendipitously occurred within the instrument's field of view. More than 64,000 photons >0.2 TeV were detected within the first 3000 seconds. The TeV flux began several minutes after the GRB trigger and then rose to a peak ~10 seconds later. This was followed by a decay phase, which became more rapid ~650 seconds after the peak. We interpret the emission using a model of a relativistic jet with half-opening angle of ~0.8°. This is consistent with the core of a structured jet and could explain the high isotropic energy of this GRB.